Created
September 25, 2016 09:16
-
-
Save oneandoneis2/9f319ce50895e144a91b0a85851dad55 to your computer and use it in GitHub Desktop.
Lispy list iteration in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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