我正在研究clojure.core的来源.
(defmacro if-not
([test then] `(if-not ~test ~then nil))
([test then else]
`(if (not ~test) ~then ~else)))
Run Code Online (Sandbox Code Playgroud)
至于第二种形式,为什么不呢
([test then else] `(if ~test ~else ~then)
我正在浏览clojure.core的来源:
(defmacro ->>
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x))]
(recur threaded (next forms)))
x)))
Run Code Online (Sandbox Code Playgroud)
在第7行,为什么不呢
(with-meta `(~@form ~x) (meta form))
Run Code Online (Sandbox Code Playgroud) 只要abs
和avg
定义:
(defn sqrt [x]
(letfn [(s [guess]
(if (good-enough? guess)
guess
(s (improve guess))))
(good-enough? [guess]
(< (abs (- (square guess) x)) 0.0001))
(improve [guess]
(avg (/ x guess) guess))]
(s 1.0)))
Run Code Online (Sandbox Code Playgroud)
请忽略一下我在这里重新发明轮子.:)这只是一个例子.
如果是这样,有没有办法绕过这个,所以每次调用函数时都不会一次又一次地绑定名称,并且不在函数外引入更多名称?
我正在浏览clojure.core的来源:
(defmacro and
([] true)
([x] x)
([x & next]
`(let [and# ~x]
(if and# (and ~@next) and#))))
Run Code Online (Sandbox Code Playgroud)
为什么不呢
(defmacro and
([] true)
([x] x)
([x & next]
`(if ~x (and ~@next) ~x)))
Run Code Online (Sandbox Code Playgroud) clojure ×4