- or download this
my @list = qw/ 7 5 2 2 /;
my $prod = shift @list;
$prod *= $_ foreach @list;
- or download this
# bad. Alters @_
sub head { shift }
# good. Preserves @_
sub head { $_[0] }
- or download this
i = j + 1
j = 6
- or download this
prodList lst =
if (length lst)==1 then head lst
else head lst*(prodList (tail lst))
- or download this
sub head { $_[0] }
sub tail { @_[1..$#_] }
...
? head @_
: (head @_) * prod_list( tail @_ )
}
- or download this
sub factorial { prod_list( 1 .. $_[0] ) }
- or download this
#!/usr/bin/perl -w
use strict;
...
sub factorial { prod_list 1 .. $_[0] }
print factorial shift;
- or download this
sub factorial {
my $result = 1;
$result *= $_ for 1 .. $_[0];
$result
}
- or download this
@new_list =
map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map { [ $_, expensive_func( $_ ) ] }
@old_list;
- or download this
# who says Perl has too much punctuation? :)
sub prod_list { 1 == @_ ? $_[0] : $_[0] * prod_list( @_[1..$#_] ) }