简单条件

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

(defn get-monitor-size [] 24)

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

;; case 测试常量,用列表可把多个常量归入同一分支。
;; 常量并不会被求值,而是按字面量处理。
;; 末尾不带测试常量的那个形式就是默认分支
(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)

;; 没有那个默认分支时,未知的值会抛出异常
(println "case 99:"
         (try (case 99 (14 15) "too small")
              (catch Exception e (str "threw: " (ex-message e)))))

;; 范围是条件而不是常量,所以要交给 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"))