小编dam*_*onh的帖子

为什么if-not调用"not"而不仅仅是反转参数?

我正在研究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

7
推荐指数
1
解决办法
158
查看次数

关于 - >>的来源

我正在浏览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)

clojure

6
推荐指数
1
解决办法
95
查看次数

每次调用`sqrt`时,`letfn`中定义的绑定是否会被更新?

只要absavg定义:

(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

5
推荐指数
1
解决办法
126
查看次数

关于和宏的来源

我正在浏览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

3
推荐指数
1
解决办法
67
查看次数

标签 统计

clojure ×4