I was curious to see if I could make a data structure using nothing but lambdas, and I came up with these JavaScript snippets:
(H)=>((F)=>F(F)())((S,V,P)=>(X)=>X?S(S,X,S(S,V,P)):(H(V),P||S(S)))
(H)=>((F)=>F(F)())((Q,V,N)=>(X)=>X?Q(Q,V,Q(Q,X,N)):(H(V),N||Q(Q)))
Try this in your browser debugger:
(...either snippet here...)(console.log)(1)(2)(3)()(4)()()(5)()()()()()()(8)()()
😱 1
Dan Cook
01/08/2021, 5:39 AM
And here's my attempt at a Turing machine
😠1
e
elbear
01/08/2021, 11:14 AM
is this Church encoding?
d
Dan Cook
01/08/2021, 5:05 PM
It's JavaScript syntax. Which might happen to be close? The c?t:f is the ternary conditional operator, which in a LISP is more like: (IF, c, (t), (f))
I suppose it's similar in concept, but not quite the same thing. At each step, you can pass parameters, and the result is the new "state", as a function of the previous state and the parameters.
Dan Cook
01/11/2021, 9:15 PM
The functions take themselves as an argument, to allow for recursive calls. That's not necessary for recursion on most languages, but only if you use assignment or "declarations". Here I was doing it without either, all as one single inline expression.