考虑以下代码片段:
(defn foo []
(let [line (apply str (repeat 2 \.))
lines (into [] (repeat 2 line))]
(for [x lines] (println x)) ; this doesn't print
(loop [remaining [[1 2] [3 4]]]
(if (empty? remaining)
(for [y lines] (println y)) ; but this does print
(recur (rest remaining))))))
运行的输出foo
是:
..
..
(nil nil)
因此,注释了此内容的行不会打印任何输出。事实上,我可以删除该行并获得相同的输出。
我期望得到的输出是:
..
..
..
..
(nil nil)
当我删除整个循环/重复业务时,该行按预期工作:
(defn foo []
(let [line (apply str (repeat 2 \.))
lines (into [] (repeat 2 line))]
(for [x lines] (println x)))) ; now it prints
产量
..
..
(nil nil)
正如预期的那样。为什么 loop/recur 会阻止 for/println 生成输出?