gpt4 book ai didi

clojure - 为什么 Clojure 中的这两个函数有不同的输出?

转载 作者:行者123 更新时间:2023-12-02 16:13:09 24 4
gpt4 key购买 nike

为什么这两个函数有不同的返回值?

(filter (and #(= (rem % 2) 1)
#(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (0 3 6 9 12...

(filter #(and (= (rem % 2) 1)
(= (rem % 3) 0))
(take 100 (iterate inc 0))) ;=> (3 9 15 21...

带有两个匿名函数的表单有效吗?

最佳答案

扩展其他答案...

您无意中发现了 Clojure 的一个“特性”,恕我直言,它造成的麻烦多于它的值(value)。

Clojure 中的“真实”是什么?

考虑一下 Clojure 认为“真实”的东西:

(defn truthy?
[x]
(if x
:yes
:no))

(dotest
(is= :yes (truthy? true))
(is= :yes (truthy? 1))
(is= :yes (truthy? :a))

(is= :no (truthy? nil))
(is= :no (truthy? false)))

请注意,函数对象也是“真实的”:

(dotest 
(let [fn-obj-1 (fn [x] (+ x 1))
fn-obj-2 #(+ % 1)] ; shorthand, equiv to fn-obj-1
(is= :yes (truthy? fn-obj-1))
(is= :yes (truthy? fn-obj-2))
(is= 42 (fn-obj-1 41)) ; call the fn
(is= 42 (fn-obj-2 41)) ; call the fn
))

宏的“棘手”结果

and 宏 ( source code ) 并不像您预期​​的那样只返回一个 bool 值。相反,它返回“导致”结果的项目:

(dotest 
(is= 3 (and 1 2 3)) ; 1
(is= :a (and 99 :a)) ; 2

(is= nil (and nil :a)) ; 3
(is= false (and false 88))) ; 4

因此,如果所有项目都是“真实的”, 将返回最后一个,因为它是“决定因素”(案例 1 和 2)。如果存在一个或多个“假”值,and 返回找到的第一个值,因为它是“决定因素”(案例 3 和 4)。

就个人而言,我从不使用 Clojure 的这个功能。恕我直言,这是一种使代码难以正确破译的“技巧”。

宏是如何工作的?

为了将“决定因素”返回给你,and 宏展开成这样的代码:

; (and 99 :a)
(let [x 99]
(if x
; recursive invocation: (and :a)
x))

因此,如果 99 为 false 或 nil,它就会返回给您。由于 99 是真值,and 将使用下一个值 :a 递归,如下所示:

; (and :a)
:a

由于此递归级别中只有一项,因此它一定是“决定因素”,and 宏只是将原始值返回给您。

记住 and 是一个宏

这意味着它在编译时运行(每个宏都被恰本地视为“编译器扩展”)。在您的第一种情况下,您的代码如下所示:

(filter (and f1 f2) ...)

其中 f1f2 都是函数。由于两者都是“真实的?”, 将最后一项返回给您。由于上面用 let 等重写的代码发生在编译时,你的代码被转换成:

(filter 
(let [x f1]
(if x
f2
f1))
...)

因为 f1 是真值,所以可以简化为

(filter 
f2
...)

f2 就是 #(= (rem % 3) 0)。所以我们看到 [0 3 6 ...] 返回。


最终答案

由于 Clojure 的“怪癖”,您的第一个 filter 不会生成编译器错误(尽管我认为它应该)。第二种形式做你想做的,因为 filter 期望使用 1 个函数来决定保留哪些项目。


以上是根据this template project .

关于clojure - 为什么 Clojure 中的这两个函数有不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67498657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com