Sunday, May 29, 2011

Are Perl and Erlang functional enough for Nemo?

I have read interesting litmus test for a functional language from stackoverflow user Nemo. Just for curiosity there is mine try for perl:
sub Inc{1+shift}
sub Thrice {
  my $f=shift;
  sub {
    $f->($f->($f->(shift)))
  }
}
$\="\n";
  print
for Thrice(\&Inc)->(0),
    Thrice(Thrice(\&Inc))->(0),
    Thrice(\&Thrice)->(\&Inc)->(0)
and Erlang:
1> Thrice = fun(F) -> fun(X) -> F(F(F(X))) end end.
#Fun
2> Inc = fun(X) -> 1+X end.
#Fun
3> (Thrice(Inc))(0).
3
4> (Thrice(Thrice(Inc)))(0).
9
5> ((Thrice(Thrice))(Inc))(0).
27
Both seems functional enough for mine personal taste.

No comments: