Created
October 5, 2015 15:35
-
-
Save calebsmith/e354546609075dfa6cc8 to your computer and use it in GitHub Desktop.
Church numbers and pairs in Clojure (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; This uses only fn and def in Clojure to create non-negative integers and pairs. | |
; (This restricts the language to only using lambda calculus.) | |
; Church numerals | |
(defn succ [n] | |
(fn [f] | |
(fn [x] | |
(f ((n f) x))))) | |
(def zero | |
(fn [f] | |
(fn [x] x))) | |
(def one (succ zero)) | |
(defn plus [a b] | |
(fn [f] | |
(fn [x] | |
((a f) ((b f) x))))) | |
; Church pairs | |
(defn cns [x y] | |
(fn [m] | |
(m x y))) | |
(defn car [c] | |
(c (fn [x y] x))) | |
(defn cdr [c] | |
(c (fn [x y] y))) | |
; Display functions | |
; N.B. - These "cheat" a little so we can see what we're working with | |
(defn display-church-num [f] | |
(when (not (nil? f)) | |
(println ((f inc) 0)))) | |
(defn display-church-list [lst] | |
(loop [lst lst] | |
(when (not (nil? lst)) | |
(display-church-num (car lst)) | |
(recur (cdr lst))))) | |
; testing | |
(display-church-list (cns one (cns zero (cns (succ one) nil)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment