gpt4 book ai didi

clojure - 如何在 Clojure 中编写这段代码

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

如果我有这样的东西:

int foo() {
if(somecondition) {
// some code
if(cond2) return -1;
// some code again
}
if(cond3){
// something again
}
return bar();
}

如何在 Clojure 中表达它?

不应该写成 if_elseif,因为 somecondition 和 cond3 都可能为 true,并且都可能被执行。

根据下面的建议,我建议另一种解决方案。请告诉我这是否正确:

(defn foo []
(if (somecondition)
( (some-code) (if (cond2) -1 (some-code-again)) )
1 ; empty statement
)
(if (cond3) (something-again) 1)
(bar))

最佳答案

您可能会说这是一种不好的风格,因为很难理解控制流,当然除非这种模式用于返回错误条件(这在 C 中很常见)。 Clojure 支持异常。如果您确实想将控制发送到其他地方,请使用它们。

关于clojure - 如何在 Clojure 中编写这段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1879128/

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