Skip to content

Instantly share code, notes, and snippets.

@masak
Created April 25, 2010 15:25

Revisions

  1. masak revised this gist Apr 25, 2010. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion cloning closures everywhere works
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    $ cat church

    # church 0 = \f -> \x -> x

    multi church(0) {
    @@ -22,4 +24,7 @@ multi plus(&m, &n) {
    pir::clone(-> &f { pir::clone(-> $x { &m(&f)(&n(&f)($x)) })})
    }

    say unchurch(plus(church(2), church(3)));
    say unchurch(plus(church(2), church(3)));

    $ perl6 church
    5
  2. masak created this gist Apr 25, 2010.
    25 changes: 25 additions & 0 deletions cloning closures everywhere works
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # 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)));