fun fold f v nil = v | fold f v (x::xs) = f(x,fold f v xs) val plus = fn (a,b) => a + b val sumlist = fold plus 0 val sum = sumlist([5,6,7,8,9])

and I set out to write the same code in Perl.

sub foldd { my $f = shift; sub { my $v = shift; sub { my $list = shift; if ($list->{'type'} eq 'EMPTY') { return $v; } else { return $f->($list->{'head'}, foldd($f)->($v)->($list->{'tail'})); } } } } my $fold = \&foldd; my $plus = sub { shift(@_) + shift(@_); }; my $sumlist = $fold->($plus)->(0); my $sum = $sumlist->({ 'type' => 'CONS', 'head' => 5, 'tail' => { 'type' => 'CONS', 'head' => 6, 'tail' => { 'type' => 'CONS', 'head' => 7, 'tail' => { 'type' => 'CONS', 'head' => 8, 'tail' => { 'type' => 'CONS', 'head' => 9, 'tail' => { 'type' => 'EMPTY' } } } } } }); print $sum;

Observations: My SML code is much longer than it needs to be; it could have been written simply as "foldl op+ 0 [5,6,7,8,9]". Even without the long list of numbers, Perl is much longer. Moreover, it was also significantly more difficult to debug. Without SML's static type checking, Perl would do things like cast a function pointer into an integer and feed it to the plus function.

So even if Perl could technically support functional programming, I highly recommend against it.