How do you return from a function early in Clojure?

后端 未结 8 1265
不知归路
不知归路 2020-12-08 13:48

Common Lisp has return-from; is there any sort of return in Clojure for when you want to return early from a function?

8条回答
  •  借酒劲吻你
    2020-12-08 14:15

    As an alternative you could use cond . And if on some conditions you would need to evaluate multiple expressions use do. Here is an example:

    (defn fact [x]
      (cond
        (< x 0) (do (println (str x " is negative number"))
                    (throw (IllegalArgumentException. "x should be 0 or higher")))
        (<= x 1) 1
        :else (* x (fact (- x 1)))))
    
    

提交回复
热议问题