Education‎ > ‎Closer to Clojure‎ > ‎

Closer to Clojure 03: Conditional

You can use '(if condition then-expr else-expr)' syntax for conditional. Use macro '(when condition expr1 expre2 ...)' when you don't need else-expr.

There is a macro if-let, which is an elegant version of evil 'if' in C.

user=> (defn process-next [waiting-line]
  (if-let [name (first waiting-line)]
    (println name "is next")
    (println "no waiting")))
user=> (process-next '("Jeremy" "Amanda" "Tami"))
Jeremy is next
user=> (process-next '())
no waiting

There also a macro 'when-let', a when-version of if-let.

Reference:
http://java.ociweb.com/mark/clojure/article.html
条件分岐は (if condition then-expr else-expr) 構文を使います.else節が不要なときはマクロ (when condition expr1 expr2 ...) を使います.

マクロ if-let があります.C言語の邪悪な習慣をエレガントにしたものです.

user=> (defn process-next [waiting-line]
  (if-let [name (first waiting-line)]
    (println name "is next")
    (println "no waiting")))
user=> (process-next '("Jeremy" "Amanda" "Tami"))
Jeremy is next
user=> (process-next '())
no waiting

whenバージョンのマクロ when-let もあります.

参考
http://java.ociweb.com/mark/clojure/article.html
Comments