【问题标题】:If else statements in clojureclojure 中的 if else 语句
【发布时间】:2016-09-16 19:11:18
【问题描述】:

我有一个 if else 条件,我想在 clojure 中实现......但不知道如何处理多个 if

我的代码是这样的

{
if(x=a)
{
you return sol1
} else
{
sol 3
}
and if(x=b)
{
return sol2
}
else
{
sol 3-same as first cond
}
}

这是一些在 3 中寻找最大整数的例子。 我如何在 clojure 中做到这一点?

【问题讨论】:

  • 你能解释一下你想在这里编码什么行为吗?有时您使用 sol 作为返回值,有时不使用,并且不清楚您要使用 and 做什么。
  • 第一次失败时返回的值。
  • 您是否要求 3 路条件?您可以使用cond
  • @joey,你看过cond(即clojuredocs.org/clojure.core/cond)吗?
  • 谢谢大家。我已经看到 cond 函数并且它有效!

标签: clojure


【解决方案1】:

如果我正确理解了您的代码,请选择以下内容。它们都有相同的行为。

(if (= x a)
  sol1
  (if (= x b)
    sol2
    sol3))

(cond
  (= x a) sol1
  (= x b) sol2
  :else   sol3)

(condp = x
  a sol1
  b sol2
  sol3)

【讨论】:

  • 太完美了!谢谢 :) 我刚刚看到 cond 函数
猜你喜欢
  • 1970-01-01
  • 2019-04-09
  • 2015-10-28
  • 2017-02-05
  • 1970-01-01
  • 2015-07-06
  • 2012-08-05
  • 1970-01-01
相关资源
最近更新 更多