Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Last active December 16, 2015 13:49
Show Gist options
  • Save sycobuny/5444365 to your computer and use it in GitHub Desktop.
Save sycobuny/5444365 to your computer and use it in GitHub Desktop.
A perl rewrite of the second base list implementation from http://stevelosh.com/blog/2013/03/list-out-of-lambda/
#!/usr/bin/env perl
use v5.14;
my $empty_list = sub {
shift->(undef, undef, 1);
};
my $prepend = sub {
my $el = shift;
my $list = shift;
sub { shift->($el, $list, 0) }
};
my $head = sub {
shift->(sub { shift })
};
my $tail = sub {
shift->(sub { shift; shift })
};
my $is_empty = sub {
shift->(sub { shift; shift; shift })
};
# example of output to verify it works
my $names = $prepend->('Alice', $prepend->('Bob', $prepend->('Candice',
$empty_list)));
say $is_empty->($names) . " should be 0";
say $head->($names) . " should be Alice";
say $tail->($names) . " should be a coderef";
say $head->($tail->($names)) . " should be Bob";
#!/usr/bin/env perl
use v5.14;
use perl5i::2;
no strict 'vars';
$empty_list = func($selector) {
$selector->(undef, undef, 1);
};
$prepend = func($el, $list) {
func($selector) { $selector->($el, $list, 0) }
};
$head = func($list) {
$list->(func($h) { $h })
};
$tail = func($list) {
$list->(func ($h, $t) { $t })
};
$is_empty = func($list) {
$list->(func($h, $t, $e) { $e })
};
# example of output to verify it works
my $names = $prepend->('Alice', $prepend->('Bob', $prepend->('Candice',
$empty_list)));
say $is_empty->($names) . " should be 0";
say $head->($names) . " should be Alice";
say $tail->($names) . " should be a coderef";
say $head->($tail->($names)) . " should be Bob";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment