I was curious to see if I could make a data struct...
# of-functional-programming
d
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
And here's my attempt at a Turing machine
😭 1
e
is this Church encoding?
d
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))
e
this is what I meant by Church encoding https://en.wikipedia.org/wiki/Church_encoding
d
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.
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.