Gangmax Blog

Clojure Notes

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
;1.  "let" usage in one form:
=> (let [x 1 y (+ x 2)] (println (+ x y)) (println (- x y)))
4
-2
nil

;2. "if" usage in some forms:
; 1. Only "false" and "nil" is "false", anything else in clojure is "true";
; 2. The value of the selected sub expression is the value of the "if" form.
(if (not nil) true false)
true
user=> (if 0 true false)
true
user=> (if -1 true false)
true
user=> (if nil true false)
false

;3. The difference between "let" and "binding":
; 1. For "binding", the binding can be used in the form who created
; the "binding" and can also be used in the functions inside the
; form; for "let", the binding cannot be used outside "let" while
; inside the "form" who created the "let", cannot be used in the
; functions inside the form who created the "let" either.
; 2. "binding" can only be used on "dynamic var".
user=> (let [x 1 y (+ x 1)] (- x y))
-1
user=> (binding [x 1 y (+ x 1)] (- x y))
CompilerException java.lang.RuntimeException: Unable to resolve var: x in this context, compiling:(NO_SOURCE_PATH:1:1)
user=> (def ^:dynamic x 1)
#'user/x
user=> (def ^:dynamic y (+ 1 1))
#'user/y
user=> (binding [x 1 y (+ x 1)] (- x y))
-1

Comments