gpt4 book ai didi

if-statement - clojure if-then-else?

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

这应该是一个非常微不足道的问题。我是 Clojure 新手,并根据以下内容编写此 if-then-else: http://clojure.org/special_forms#Special%20Forms--%28if%20test%20then%20else?%29

但是,我不断收到此错误:

java.lang.Exception:if 参数过多 (NO_SOURCE_FILE:424)

// This code has infinite recursion bugs; but it should compile!
(defn sumdown [sum x]
(
if (foo x)
(do
(recur (+ sum x) (dec x))
)
else do(
(recur (+ sum x) (dec x))
)

)
)

最佳答案

在模板中:

(if test then else?)

thenelse都不代表关键字,而是实际的表达式。 ? 表示 else 表达式是可选的。

评估测试条件,并根据其值评估适当的分支表达式,并将其“返回”为整个 if 表达式的值。

例如,这将定义一个绝对值函数:

(defn abs [x]
(if (< x 0)
(- x)
x))

关于if-statement - clojure if-then-else?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8622566/

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