public
Last active

  • Download Gist
cloning closures everywhere works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
$ cat church
 
# church 0 = \f -> \x -> x
 
multi church(0) {
pir::clone(-> &f { pir::clone(-> $x { $x }) });
}
 
# church n = \f -> \x -> f (church (n-1) f x)
 
multi church($n) {
pir::clone(-> &f { pir::clone(-> $x { &f(church($n - 1)(&f)($x)); }) });
}
 
# unchurch n = n (\x -> x + 1) 0
 
sub unchurch(&c) {
&c(pir::clone(-> $x { $x + 1 }))(0);
}
 
# plus m n = \f -> \x -> m f (n f x)
 
multi plus(&m, &n) {
pir::clone(-> &f { pir::clone(-> $x { &m(&f)(&n(&f)($x)) })})
}
 
say unchurch(plus(church(2), church(3)));
 
$ perl6 church
5

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.