2013-12-12 37 views
2

所以我想寫以下Clojure中(假設下面返回boolean變量的所有方法)正確的方法來編寫條件代碼Clojure中

def some_method(a, b) 
    if (call_this_method()) 
    then_call_this_method() 
    else 
    new_method() 
end 

我得到了什麼是這樣的:

(defn some-method [a b] 
    (if (call_this_method) 
    :then (then-call-this-method) 
    :else (new-method))) 

我我對clojure很新,所以我不確定這是否是解決這個問題的正確方法。有不同的方法嗎?

+0

有在clojuredocs.org例子,從http://clojuredocs.org/clojure_core/clojure.core/if – edbond

回答

5

if基本上需要3個PARAMS,[條件是什麼到運行,如果真可選 - 運行 - 如果假]

(defn some-method 
    "My function does ..." 
    [ a b ] 
    (if (call-this-method) 
     (then-call-this-method) 
     (new-method))) 
+1

如果啓動你需要在'then'或'else'分支中使用'(do(function1)(function2))'''中的幾個子句 – edbond

5

您可以用Clojure使用if以及

(if test then-code else-code) 

或者cond這更像是開關

(cond 
    test-A run-if-A 
    test-B run-if-B 
    ... 
    :else else-code) 

如果喲u想這樣做

if(foo) bar; 

然後你會寫

(when foo bar) 
相關問題