Created
June 2, 2010 20:06
-
-
Save Chouser/422908 to your computer and use it in GitHub Desktop.
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
(def CONS | |
(fn [a b] | |
(fn [x] | |
(if (= x -1) | |
'CONS | |
(if (= x 0) | |
a | |
b))))) | |
(def FIRST | |
(fn [x] | |
(if x | |
(if (= (x -1) 'CONS) | |
(x 0) | |
(if (x) | |
((x) 0)))))) | |
(def REST | |
(fn [x] | |
(if x | |
(if (= (x -1) 'CONS) | |
(x 1) | |
(if (x) | |
((x) 1)))))) | |
(def SEQ | |
(fn [x] | |
(if x | |
(if (= (x -1) 'CONS) | |
x | |
(if (x) | |
(SEQ (x))))))) | |
(def LAZY-SEQ | |
(fn [f] | |
(fn ([x] (if (= x -1) | |
'LAZY-SEQ)) | |
([] (f))))) | |
(def MAP | |
(fn [f s] | |
(LAZY-SEQ (fn [] | |
(if (SEQ s) | |
(CONS (f (FIRST s)) | |
(MAP f (REST s)))))))) | |
(FIRST (MAP inc (MAP inc (CONS 1 nil)))) | |
;=> 3 | |
(FIRST (REST (MAP inc (CONS 1 nil)))) | |
;=> nil | |
(def DORUN | |
(fn [s] | |
(if (SEQ s) | |
(recur (REST s))))) | |
(def PRN | |
(fn [s] | |
(print "(") | |
(DORUN (MAP (fn [x] | |
(print x) | |
(print " ")) | |
s)) | |
(print ")\n"))) | |
; Whoops, doesn't cache: | |
(PRN (CONS 1 (CONS 2 (CONS 3 nil)))) | |
; (1 1 1 1 2 2 2 2 3 3 3 3 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment