S combinator in Erlang

701 views Asked by At

I'm starting to learn lambda calculus and I need to implement I, S, K combinators in Erlang. Of course, S, K, I stands for:

S = λxyz.xz(yz) K = λxy.x I = λx.x

I have no problem understanding I=SKK transformation on paper (like presented here: To prove SKK and II are beta equivalent, lambda calculus) but it seems that I don't understand it when it comes to functional languages and high-order functions...

I managed to do I and K (lets say in module test):

i(X) -> X.
k(X) -> fun(Y) -> X end.

Also I know how to run K x (K x) (SKK x = K x (K x))

kxk(X) -> (k(X))(k(X)).

But I can't get it around to write S combinator. I tried:

s(X) -> fun (Y) -> fun(Z) -> X,Z (Y,Z) end end.

But still, I'm not able to transform SKK x into x

I try to run it like this:

skkx(X) ->  s((k((k(X))))).

Any help would be appreciated, as I'm completely lost.

1

There are 1 answers

3
RichardC On BEST ANSWER

From the Erlang shell:

1> I = fun (X) -> X end.
#Fun<erl_eval.6.80247286>
2> K = fun (X) -> fun (Y) -> X end end.
#Fun<erl_eval.6.80247286>
3> S = fun (X) -> fun (Y) -> fun (Z) -> (X(Z))(Y(Z)) end end end.
#Fun<erl_eval.6.80247286>
4> ((S(K))(K))(42).
42

Or as functions in a module:

i(X) -> X.
k(X) -> fun(Y) -> X end.
s(X) -> fun (Y) -> fun (Z) -> (X(Z))(Y(Z)) end end.