Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created June 2, 2010 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Chouser/422908 to your computer and use it in GitHub Desktop.
Save Chouser/422908 to your computer and use it in GitHub Desktop.
(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