作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(defn square [x]
(do
(println (str "Processing: " x))
(* x x)))
(println (map square '(1 2 3 4 5)))
为什么输出
(Processing: 1
Processing: 2
1 Processing: 3
4 Processing: 4
9 Processing: 5
16 25)
不是
(Processing: 1
1 Processing: 2
4 Processing: 3
9 Processing: 4
16 Processing: 5
25)
?
最佳答案
因为 map
很懒。它在幕后使用了lazy-seq,pre-caches the result of rest
。因此,当您的代码获取 map
序列的第一个值时,您会看到两个 println
语句出现。
另请参阅此博文:Lazy Sequences
关于clojure - 惰性序列的实现时机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455008/
我是一名优秀的程序员,十分优秀!