开发者

S combinator in Erlang

开发者 https://www.devze.com 2023-04-13 06:08 出处:网络
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:

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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消