#!/usr/local/bin/perl5
sub tail {
shift(@_);
@_;
}
# @_[0] is far more efficient than head(@_);
sub head {
shift(@_);
}
sub list_length {
$#_;
}
sub prob_six {
(list_length(@_) < 0 && return 0);
(((head(@_) & 1) * head(@_)) + prob_six(tail(@_)));
}
# Is perl's ?: an if-then-else? or does it conform because it is just a value?
# (Ken Anderson said it was okay). I only use it for bounds-checking,
# anyway.
sub prob_seven {
($#_ < 0? 0: (((@_[0] & 1) * @_[0]) + prob_seven(tail(@_))));
# translation:
# is my argument-list an empty list?
# yes: 0
# no: ((head(list)& 01) * head(list)) + sum_odd(tail(list))
}
print "solution: " . prob_seven(1, 2, 3, 4, 5, 6, 7, 8, 9) . "\n";