# LISP - 決策

LISP提供了以下類型的決策構造。點擊以下鏈接，查看其詳細信息。

Construct

cond

if

if結構有多種形式。在最簡單的形式，它後面跟着一個測試條，測試操作和一些其它相應措施(次)。如果測試子句的值爲true，那麼測試的動作被執行，否則，由此產生的子句求值。

when

case

## LISP的cond特殊構造

cond的語法是：

(cond (test1 action1) (test2 action2) ... (testn actionn))

(setq a 10) (cond ((> a 20) (format t "% a is less than 20")) (t (format t "% value of a is ~d " a)))

value of a is 10

## if結構

if的語法：

(if (test-clause) (<action1) (action2))

(setq a 10) (if (> a 20) (format t "% a is less than 20")) (format t "% value of a is ~d " a)

value of a is 10

if子句後面可以跟一個可選的then子句：

(setq a 10) (if (> a 20) then (format t "% a is less than 20")) (format t "% value of a is ~d " a)

a is less than 20 value of a is 10

(setq a 100) (if (> a 20) (format t "% a is greater than 20") (format t "% a is less than 20")) (format t "~% value of a is ~d " a)

a is greater than 20 value of a is 100

when構造，

when宏的語法：

(when (test-clause) (<action1) )

(setq a 100) (when (> a 20) (format t "% a is greater than 20")) (format t "% value of a is ~d " a)

a is greater than 20 value of a is 100

case構造

case結構實現像cond結構多個測試動作語句。但是，它會評估的鍵形式，並允許根據該鍵的形式評價多個動作語句。

The template for CASE is:

(case (keyform) ((key1) (action1 action2 ...) ) ((key2) (action1 action2 ...) ) ... ((keyn) (action1 action2 ...) ))

(setq day 4) (case day (1 (format t "% Monday")) (2 (format t "% Tuesday")) (3 (format t "% Wednesday")) (4 (format t "% Thursday")) (5 (format t "% Friday")) (6 (format t "% Saturday")) (7 (format t "~% Sunday")))

Thursday