Lisp - when 結構



when 宏後面是一個 test 子句,該子句可計算為 t 或 nil。如果 test 子句計算為 nil,則不計算任何形式並返回 nil,但如果測試結果為 t,則執行 test 子句後面的 action。

when 宏的語法 −

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

示例

建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; set a as 100
(setq a 100)
; check if a is greater than 20
(when (> a 20)
   ; print the statement is above statement is true
   (format t "~% a is greater than 20"))
; print the statement
(format t "~% value of a is ~d " a)

輸出

當你單擊“執行”按鈕,或鍵入 Ctrl+E 時,LISP 立即執行它,返回的結果是 −

a is greater than 20
value of a is 100 

示例

更新檔案 main.lisp,並在其中鍵入以下程式碼。

main.lisp

; set a as 10
(setq a 10)
; check if a is greater than 20
(when (> a 20)
   ; print the statement is above statement is true
   (format t "~% a is greater than 20"))
; print the statement
(format t "~% value of a is ~d " a)

輸出

當你單擊“執行”按鈕,或鍵入 Ctrl+E 時,LISP 立即執行它,返回的結果是 −

value of a is 10 
lisp_decisions.htm
廣告
© . All rights reserved.