为什么我的 println (示例的最后一行)不像列表理解?我正在尝试使用以下代码来解决 Clojure 中 Project Euler 的问题 #2:
(def fibsq [0 1])
(defn second-to-last [sq]
(nth sq (- (count sq) 2)))
(second-to-last fibsq)
(defn add-last-two [sq]
(+ (last sq) (second-to-last sq)))
(defn add-to-fibsq [sq]
(if (< (add-last-two sq) 100)
(add-to-fibsq (conj sq (add-last-two sq)))
sq))
;; putting it all together
(add-to-fibsq fibsq)
(println [x (add-to-fibsq fibsq) :when (even? x)])
我得到的输出如下:
(定义 ||[0 1]|| [0 1])
(defn 倒数第二个 [||[0 1 1 2 3 5 8 13 21 34 55 89]||] (nth ||[0 1 1 2 3 5 8 13 21 34 55 89]|| (- (计数 ||[0 1 1 2 3 5 8 13 21 34 55 89]||) 2)))
(倒数第二个||[0 1]||)|| => 0||
(defn add-last-two [||[0 1 1 2 3 5 8 13 21 34 55 89]||] (+ (last ||[0 1 1 2 3 5 8 13 21 34 55 89]||) (倒数第二个 ||[0 1 1 2 3 5 8 13 21 34 55 89]||))
(defn add-to-fibsq [||[0 1 1 2 3 5 8 13 21 34 55 89]||] (if (< (add-last-two ||[0 1 1 2 3 5 8 13 21 34 55 89]||) 100) (add-to-fibsq (conj ||[0 1 1 2 3 5 8 13 21 34 55 89]|| (add-last-two ||[0 1 1 2 3 5 8 13 21 34 55 89]||))) ||[0 1 1 2 3 5 8 13 21 34 55 89]||))
;; 把它们放在一起 (add-to-fibsq ||[0 1]||)|| => [0 1 1 2 3 5 8 13 21 34 55 89]||
(println [x (add-to-fibsq ||[0 1]||) :when (even?x)])|| => java.lang.RuntimeException: Unable to resolve symbol: x in this context, 编译:(NO_SOURCE_PATH:19:2)||
这不是列表推导的工作方式吗?