Skip to content

Instantly share code, notes, and snippets.

@oneandoneis2
Created September 25, 2016 09:16
Show Gist options
  • Save oneandoneis2/9f319ce50895e144a91b0a85851dad55 to your computer and use it in GitHub Desktop.
Save oneandoneis2/9f319ce50895e144a91b0a85851dad55 to your computer and use it in GitHub Desktop.
Lispy list iteration in Perl
#!/usr/bin/env perl
# Lisp-like lists in perl, made entirely out of functions.
sub cons { my ($h,$t)=@_; return sub{ $fn=shift;$fn->($h,$t)} }
sub head { my $lst=shift; $lst->( sub{ my($h,$t)=@_; return $h} );}
sub tail { my $lst=shift; $lst->( sub{ my($h,$t)=@_; return $t} );}
sub list { my $thing = shift; if (defined $thing) { return cons($thing, list(@_)) } }
sub listmap {
my ($fn,$lst)=@_;
my $it;
$it = sub {
my ($h,$t) = @_;
$fn->($h);
if ($t) {
$t->($it)
}
};
$lst->($it)
}
my $list = list(1,2,3,4,5);
listmap( sub { print "$_[0]\n" }, $list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment