-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelude.slf
59 lines (50 loc) · 1.28 KB
/
prelude.slf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(defmacro defun
(name args body)
`(def ~name (fn ~args ~body)))
(defun load-file
(name)
(eval (read-string (slurp name))))
; Unique symbols generation
(def *__gensym-counter__* (atom 0))
(defun gensym
()
(symbol (str "G__"
(swap *__gensym-counter__*
(fn (x) (+ 1 x))))))
(defun inc (a) (+ 1 a))
(defun dec (a) (- a 1))
(defun not (pred) (if pred false true))
(defmacro or
(& xs)
(if (empty? xs)
()
(if (= 1 (count xs))
(first xs)
(let (condvar (gensym))
`(let (~condvar ~(first xs))
(if ~condvar ~condvar (or ~@(rest xs))))))))
(defmacro and
(& xs)
(if (empty? xs)
()
(if (= 1 (count xs))
(first xs)
(let (condvar (gensym))
`(let (~condvar ~(first xs))
(if ~condvar (and ~@(rest xs)) ~condvar))))))
(defmacro cond
(& xs)
(if (> (count xs) 0)
(list 'if (first xs)
(if (> (count xs) 1)
(nth xs 1)
(throw "odd number of forms to cond"))
(cons 'cond (rest (rest xs))))
()))
(defun map
(f xs)
(if (> (count xs) 1)
(cons (f (first xs))
(map f (rest xs)))
(list (f (first xs)))))
true