Skip to content

Instantly share code, notes, and snippets.

@okkez
Created December 13, 2008 15:06
Show Gist options
  • Save okkez/35479 to your computer and use it in GitHub Desktop.
Save okkez/35479 to your computer and use it in GitHub Desktop.
Church numerals
# Church numerals
zero = lambda{|f|
lambda{|x| x }
}
one = lambda{|f|
lambda{|x|
f.call(x)
}
}
two = lambda{|f|
lambda{|x|
f.call(f.call(x))
}
}
three = lambda{|f|
lambda{|x|
f.call(f.call(f.call(x)))
}
}
p zero.call(lambda{|x| x + 1 }).call(0)
p one.call(lambda{|x| x + 1 }).call(0)
p two.call(lambda{|x| x + 1 }).call(0)
p three.call(lambda{|x| x + 1 }).call(0)
add = lambda{|a, b|
lambda{|f|
lambda{|x|
a.call(f).call(b.call(f).call(x))
}
}
}
p add.call(two, three).call(lambda{|x| x + 1 }).call(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment