gpt4 book ai didi

clojure - 为什么 if-not 调用 "not",而不是仅仅反转参数?

转载 作者:行者123 更新时间:2023-12-03 00:27:29 26 4
gpt4 key购买 nike

我正在研究 clojure.core 的源代码。

(defmacro if-not
([test then] `(if-not ~test ~then nil))
([test then else]
`(if (not ~test) ~then ~else)))

至于第二种形式,为什么不直接

([test then else] `(if ~test ~else ~then)

最佳答案

这看起来只是一种编码风格。

(if-not test then else)

(if (not test) then else)

(if test else then)

上面的代码将以同样的方式工作。有多种方法可以编写代码来完成同一件事。

if-not 宏的作者可能认为这样编写代码会更好。

(defmacro if-not
...
([test then else]
`(if (not ~test) ~then ~else)))

当我们阅读上面的这段代码时,我们可以按照ifthenelse的顺序思考,非常简单。

(defmacro if-not
...
([test then else]
`(if ~test ~else ~then)

是的,这会工作得很好。不过,从可读性角度来说,thenelse 的顺序互换了,可能会造成困惑。

这就是为什么(我猜)作者以这种方式实现 if-not

关于clojure - 为什么 if-not 调用 "not",而不是仅仅反转参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42390596/

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