# -*- ruby -*- class Proc U = proc{|f| f[f]} Y = proc{|g| proc{|f| g[f[f]] }[ proc{|f| g[proc{|y| f[f][y]}] } ] } def Proc.new_rec_u U[proc{|f| proc{|x| yield(f[f])[x] }}] end # use yield() instead of g[] def Proc.new_rec_y proc{|f| yield(f[f]) }[ proc{|f| yield(proc{|y| f[f][y]}) } ] end def Proc.new_rec Proc.new_rec_u end end def rec Proc.new_rec end if( __FILE__ == $0 ) u_fact = Proc.new_rec_u{|f| proc{|x| if( x > 0 ) x * f[x-1] else 1 end }} y_fact = Proc.new_rec_y{|f| proc{|x| if( x > 0 ) x * f[x-1] else 1 end }} p y_fact[3] p u_fact[3] ## However, we would usually use local functions as follows: fact = Proc.new{|x| _fact = Proc.new{|y| if( y > 0 ) y * _fact[y - 1] else 1 end } _fact[x] } p fact[3] # The above definition is a little similar to the following # definition in ML syntax: # # fun fact x = # let # fun _fact x = if x > 0 then x * (_fact (x - 1)) # in # _fact(x) # end end