[英]Using filter in clojure function
Why 为什么
(filter even? (range 10))
gives (0 2 4 6 8) 给出(0 2 4 6 8)
but 但
((fn [x] filter even? x)
(range 10))
gives 给
(0 1 2 3 4 5 6 7 8 9)
? ?
There is an issue in your anonymous function - let's write it in a more readable way: 您的匿名函数中有一个问题-让我们以一种更具可读性的方式编写它:
((fn [x]
(do filter
even?
x))
(range 10))
Now I think it is obvious why it returns the whole range sequence: value of (range 10)
is bound to argument x
and is returned as the result from your function. 现在,我认为很明显为什么它返回整个范围序列:( (range 10)
值绑定到参数x
并作为函数的结果返回。
The correct version would be: 正确的版本是:
((fn [x] (filter even? x))
(range 10))
;; => (0 2 4 6 8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.