Y-combinator in Ruby with REAL λ!

Posted by flori Mon, 08 Oct 2007 17:02:00 GMT

#!/usr/bin/ruby -KU
# -*- coding: utf-8 -*- 

# Do ":imap <C-l> <C-k>l*" in vim

alias λ lambda

Y = λ{ |c|
  λ{ |f| f[f] }[
    λ{ |f| c[
      λ{ |x| f[f][x] } ] } ] }

Y[ λ{ |f| λ{ |n| n < 1 ? 1 : n * f[n-1] } } ][10] # => 3628800

And they say reading ruby-core doesn't pay off! :-)

Tags , ,  | no comments

Feeling at home in Ruby (if you're coming from C)

Posted by flori Fri, 03 Aug 2007 22:13:00 GMT

class Fixnum
  alias __oldref :[]

  def [](obj)
    if Fixnum === obj
      __oldref(obj)
    else
      obj[self]
    end
  end
end

if $0 == __FILE__
  2[0] # => 0
  2[1] # => 1
  a = [2,3,5,7,11]
  a[2] # => 5
  2[a] # => 5
end

Tags , , ,  | no comments

Rail On Rails

Posted by flori Wed, 06 Jun 2007 23:47:00 GMT

I just found this link to the rail programming language in matz' blog. It has a rather beautiful layout as you can see in the following implementation of the Ackermann function:

$ 'main' (--)
 \  Read m and n as input. Just one number each for now
  \  Input is in the form "m\rn" where \r is a return character
   \-[Enter m: ]oi(!m!)[\n\]o-[Enter n: ]oii(!n!)[\n\]o-----(n)-(m)-{ackermann}-[): ](n)[,](m)[A(]oooooo--#


$ 'ackermann' (n m -- A(m,n))  A(m,n) = n+1             if m=0
 \                                    = A(m-1,1)        if m>0 and n=0
  \                                   = A(m-1,A(m,n-1)) if m>0 and n>0
   \-(!m!)-(!n!)-\
                 | 
                 /
      /---q(m)0--     
      |
     t^f        f/-(n)1s-(m)-{ackermann}-(m)1s-{ackermann}-#  
     / \-(n)0q--<
     |          t\-1-(m)1s-{ackermann}-#
     \-(n)1a-#

Yeah, it's an esoteric language. Matz sure doesn't joke when he says, that he's a programming language geek. I wonder when the rail hackers will start to code a web framework and call it Rail On Rails?

Tags , , ,  | no comments