|
|
|
|
|
by GravityLab
1048 days ago
|
|
This is very beautiful. TIL about Y combinators. Thanks for sharing. I looked it up with ChatGPT to learn more about what it is and then compared it with versions from different languages. JavaScript: const Y = f => (x => f(v => x(x)(v)))
(x => f(v => x(x)(v)));
Lisp: (define (Y f)
((lambda (x) (f (lambda (y) ((x x) y))))
(lambda (x) (f (lambda (y) ((x x) y))))))
Haskell: y :: (a -> a) -> a
y f = f (y f)
OCaml: let z f =
let fn = ref (fun x -> x) in
fn := (fun x -> f (!fn x));
!fn;;
I think I still like the Ruby the most since it's easier to grok due to the Lisp having so many parens towards the end. The Haskell is beautiful to look at too. |
|
As you might expect for a moderately complicated topic, ChatGPT is hallucinating. That is beautiful because it is not the Y combinator.
That is the Fixed-point combinator. The Y combinator is used to implement recursion when you can't directly reference the function binding.
In the haskell solution, y is used recursively on the rhs, so it's not the Y combinator, and so it's defining something different and simpler, so of course it looks different and simpler.
Do not trust ChatGPT without first giving it a brief bit of thought yourself.
And also, please don't just post ChatGPT's ramblings here if you have nothing worth adding. Most of us, I think, are here for human discussions, not to talk to chatGPT indirectly. If I wanted chatgpt's ramblings, I would go to that site instead.