简单条件

控制流 / 条件语句 / switch/case 语句

(defn get-monitor-size [] 24)

(def inch-size (get-monitor-size))

;; case tests constants, and a list groups several of them for one branch.
;; the constants are NOT evaluated - they are taken literally.
;; the trailing form without a test is the default
(def s
  (case inch-size
    (14 15) "too small"
    (16 17 18) "good for the past decade"
    (19 20 21 22 23) "for office work"
    (24 25 26 27) "great choice"
    ""))
;; s is "great choice"
(println "Monitor" inch-size "is" s)

;; without that default an unknown value throws
(println "case 99:"
         (try (case 99 (14 15) "too small")
              (catch Exception e (str "threw: " (ex-message e)))))

;; a range is a condition, not a constant, so it is a job for cond
(println "with cond:"
         (cond
           (< inch-size 16) "too small"
           (<= 16 inch-size 18) "good for the past decade"
           (<= 19 inch-size 23) "for office work"
           :else "great choice"))