gpt4 book ai didi

clojure - Clojure 中的逻辑真实性

转载 作者:行者123 更新时间:2023-12-02 15:45:32 26 4
gpt4 key购买 nike

true? 实际上说了什么?(真?0)=(如果 0“0 为真”“0 为假”)=0 为真。为什么会出现这种情况?

最佳答案

您混淆了两件事:

  • Clojure 中存在的不同,以及
  • if 及其后代处理这些值的方式。

true1 是值,它们是不同的:

(= true 1) ; false

但它们与 if 的第一个参数具有相同的效果:

(if true "Hello!" "Goodbye.") ; "Hello!"

(if 1 "Hello!" "Goodbye.") ; "Hello!"

事实上,几乎任何第一个参数都会导致 if 计算并返回其第二个参数:

(if + "Hello!" "Goodbye.") ; "Hello!"

(if *ns* "Hello!" "Goodbye.") ; "Hello!"

(if String "Hello!" "Goodbye.") ; "Hello!"

只有两个会导致if计算并返回其第三个​​参数。这两个值是 falsenil

(if false "Hello!" "Goodbye.") ; "Goodbye."

(if nil "Hello!" "Goodbye.") ; "Goodbye."

如果没有提供第三个参数,则默认为nil:

(if false "Hello!") ; nil

值之间的相同区别也适用于其他 Clojure 条件,它们直接或间接派生自 if:if-notwhen何时不,&c。它们被表示为宏,因此,就像 if 一样,它们在需要时才会评估其参数。

引用official documentation

(if test then else?)

Evaluates test. If not the singular values nil or false, evaluates and yields then, otherwise, evaluates and yields else. If else is not supplied it defaults to nil. All of the other conditionals in Clojure are based upon the same logic, that is, nil and false constitute logical falsity, and everything else constitutes logical truth, and those meanings apply throughout.

关于clojure - Clojure 中的逻辑真实性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420029/

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