Skip to content

Instantly share code, notes, and snippets.

@masak
Created April 25, 2010 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/378479 to your computer and use it in GitHub Desktop.
Save masak/378479 to your computer and use it in GitHub Desktop.
$ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment