forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep8_macros.mal
More file actions
169 lines (142 loc) Β· 2.22 KB
/
step8_macros.mal
File metadata and controls
169 lines (142 loc) Β· 2.22 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;; Testing trivial macros
(defmacro! one (fn* () 1))
(one)
;=>1
(defmacro! two (fn* () 2))
(two)
;=>2
;; Testing unless macros
(defmacro! unless (fn* (pred a b) `(if ~pred ~b ~a)))
(unless false 7 8)
;=>7
(unless true 7 8)
;=>8
(defmacro! unless2 (fn* (pred a b) `(if (not ~pred) ~a ~b)))
(unless2 false 7 8)
;=>7
(unless2 true 7 8)
;=>8
;; Testing macroexpand
(macroexpand (unless2 2 3 4))
;=>(if (not 2) 3 4)
;; Testing evaluation of macro result
(defmacro! identity (fn* (x) x))
(let* (a 123) (identity a))
;=>123
;>>> deferrable=True
;;
;; -------- Deferrable Functionality --------
;; Testing non-macro function
(not (= 1 1))
;=>false
;;; This should fail if it is a macro
(not (= 1 2))
;=>true
;; Testing nth, first and rest functions
(nth (list 1) 0)
;=>1
(nth (list 1 2) 1)
;=>2
(def! x "x")
(def! x (nth (list 1 2) 2))
x
;=>"x"
(first (list))
;=>nil
(first (list 6))
;=>6
(first (list 7 8 9))
;=>7
(rest (list))
;=>()
(rest (list 6))
;=>()
(rest (list 7 8 9))
;=>(8 9)
;; Testing or macro
(or)
;=>nil
(or 1)
;=>1
(or 1 2 3 4)
;=>1
(or false 2)
;=>2
(or false nil 3)
;=>3
(or false nil false false nil 4)
;=>4
(or false nil 3 false nil 4)
;=>3
(or (or false 4))
;=>4
;; Testing cond macro
(cond)
;=>nil
(cond true 7)
;=>7
(cond true 7 true 8)
;=>7
(cond false 7 true 8)
;=>8
(cond false 7 false 8 "else" 9)
;=>9
(cond false 7 (= 2 2) 8 "else" 9)
;=>8
(cond false 7 false 8 false 9)
;=>nil
;; Testing EVAL in let*
(let* (x (or nil "yes")) x)
;=>"yes"
;>>> optional=True
;;
;; -------- Optional Functionality --------
;; Testing nth, first, rest with vectors
(nth [1] 0)
;=>1
(nth [1 2] 1)
;=>2
(def! x "x")
(def! x (nth [1 2] 2))
x
;=>"x"
(first [])
;=>nil
(first nil)
;=>nil
(first [10])
;=>10
(first [10 11 12])
;=>10
(rest [])
;=>()
(rest nil)
;=>()
(rest [10])
;=>()
(rest [10 11 12])
;=>(11 12)
;; Testing EVAL in vector let*
(let* [x (or nil "yes")] x)
;=>"yes"
;;
;; Loading core.mal
(load-file "../core.mal")
;; Testing -> macro
(-> 7)
;=>7
(-> (list 7 8 9) first)
;=>7
(-> (list 7 8 9) (first))
;=>7
(-> (list 7 8 9) first (+ 7))
;=>14
(-> (list 7 8 9) rest (rest) first (+ 7))
;=>16
;; Testing ->> macro
(->> "L")
;=>"L"
(->> "L" (str "A") (str "M"))
;=>"MAL"
(->> [4] (concat [3]) (concat [2]) rest (concat [1]))
;=>(1 3 4)