From: Johnny Morrice Date: 2011-05-06T21:04:02+09:00 Subject: Re: Lets play a guessing game. (how to code this better?) Actually I left a bug in that due to side effects. (When will lazy functional ruby be out?) Here you go: # I have defined the combinators as constants to reflect the TRUTH and PURITY of COMPUTER SCIENCE # Z combinator defines explicit recursion Z = lambda { |f| (lambda {|x| f.call(lambda {|*ys| x.call(x).call(*ys)})}).call(lambda {|x| f.call(lambda {|*ys| x.call(x).call(*ys)})})} # The guess combinator holds a reference to itself in the 'again' variable Guess = lambda do |again| lambda do |magic| print "Choose 0-100> " n = gets.to_i if (0..100).include?(n) puts "Higher!" if nmagic if n==magic puts "You won: #{magic} is the number!" else again.call magic end else puts "0-100!" again.call magic end end end magic = rand(101).to_i puts "Guess the magic number!" Z.call(Guess).call(magic)