Chad Perrin: SOB

20 April 2007

adjusting the syntax of Fei Ling’s Y combinator

Filed under: Geek — apotheon @ 02:24

On the subject of why — or, in this case, Y . . .

At Fei Ling’s website, there’s an example of constructing a Y combinator. I looked at it, and thought “Wow, this is what people must mean when they say someone writes Ruby like Perl — or even C.” I decided to adjust very superficially to make it better match a Ruby idiom. If anyone would care to share an even better implementation, I’d be happy to see it. Here’s Fei Ling’s version:

Y = proc {

proc { |g| g[g] }.call( proc { |h|

              proc {



Here’s mine:

Y = proc do |f|
  proc {|g| g[g] }.call(
    proc do |h|
      proc {|n| (f[h[h]])[n]}

I think that’s correct, anyway. At least, irb doesn’t complain. Now, I’ll repeat Fei Ling’s question: For what, exactly, do I use this?


  1. A Y combinator is used to define anonymous recursive functions. For example, if you wanted to pass a recursive proc as an argument to a function but you do not want to define it beforehand.

    Comment by Jake — 20 April 2007 @ 12:35

  2. Shouldn’t such a thing in Ruby just be created as a plain ol’ block, though?


    array.each do |y|
      # do stuff

    Comment by apotheon — 20 April 2007 @ 02:19

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

All original content Copyright Chad Perrin: Distributed under the terms of the Open Works License