Paste number 39267: Church numerals

Paste number 39267: Church numerals
Pasted by: khigia
When:3 years, 3 months ago
Share:Tweet this! | http://paste.lisp.org/+UAR
Channel:None
Paste contents:
Raw Source | XML | Display As
% Church numerals

zero() ->
    fun(_F) ->
        fun(X) -> X end
    end.

succ(N) ->
    fun(F) ->
        fun(X) -> F( (N(F))(X) ) end
    end.

add(M, N) ->
    fun(F) ->
        fun(X) ->
            (N(F))((M(F))(X))
        end
    end.

mult(M, N) ->
    fun(F) ->
        fun(X) ->
            (M(N(F)))(X)
        end
    end.

test() ->
    L = fun(V) -> io_lib:format("f(~s)", [V]) end,
    One = succ(zero()),
    io:format("Church numeral 1: ~s~n", [(One(L))("0")]),
    Two = succ(One),
    io:format("Church numeral 2: ~s~n", [(Two(L))("0")]),
    Three = succ(Two),
    io:format("Church numeral 3: ~s~n", [(Three(L))("0")]),
    Sum = add(Two, Three),
    io:format("Addition of 2 and 3: ~s~n", [(Sum(L))("0")]),
    Prod = mult(Two, Three),
    io:format("Product of 2 and 3: ~s~n", [(Prod(L))("0")]),
    ok.

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.