Skip to content

Instantly share code, notes, and snippets.

@viebel
Created July 22, 2016 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/d751d8dbd5cc3ee2707059dd028a811c to your computer and use it in GitHub Desktop.
Save viebel/d751d8dbd5cc3ee2707059dd028a811c to your computer and use it in GitHub Desktop.
Numbers in Lambda Calculus
(defn v [x]
(list 'λ x))
(defn view [n] ((n v) 'x))
(deftype LambdaNum [n]
Object
(toString [this] (str (view this)))
(equiv [this other] (-equiv this other))
IPrintWithWriter
(-pr-writer [o writer _] (-write writer (view o)))
IEquiv
(-equiv [this other]
(if (instance? LambdaNum other)
(= (view this) (view other))
false))
IFn
(-invoke [this f]
(fn [x]
((apply comp (repeat n f)) x))))
(def two (LambdaNum. 2))
(def another-two (LambdaNum. (+ 1 1)))
(def zero (LambdaNum. 0))
[zero
two
(str two)
(= two two)
(= two another-two)
(= two three)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment