def pair(left,right)
lambda do |dyadic|
dyadic.call(left,right)
end
end
def left(pair)
pair.call(lambda {|left,right| left})
end
def right(pair)
pair.call(lambda {|left,right| right})
end
require 'rubyunit'
class WorkspaceTest < Test::Unit::TestCase
def test_simple
a_b=pair('a','b')
assert_equal('a',left(a_b))
assert_equal('b',right(a_b))
end
def test_again
a_b=pair('a', 'b')
c_a_b=pair('c', a_b)
assert_equal('c', left(c_a_b))
assert_equal('a', left(right(c_a_b)))
end
end
Friday, September 14, 2007
More Functional Fun In Ruby
I have no clue whenever this will be useful, but it was a fun exercise. Hope you enjoy it as well. It's an implementation of Lisp's cons, car, and cdr.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment