Lisp-if-construct

提供:Dev Guides
移動先:案内検索

LISP-Ifコンストラクト

*if* マクロの後には、tまたはnilと評価されるテスト句が続きます。 test句がtに評価されると、test句に続くアクションが実行されます。 nilの場合、次の句が評価されます。

ifの構文-

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

例1

main.lispという名前の新しいソースコードファイルを作成し、次のコードを入力します。

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

実行ボタンをクリックするか、Ctrl + Eを入力すると、LISPはすぐに実行し、返される結果は-

value of a is 10

例2

*if* 句の後にオプションの *then* 句を続けることができます。

main.lispという名前の新しいソースコードファイルを作成し、次のコードを入力します。

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

実行ボタンをクリックするか、Ctrl + Eを入力すると、LISPはすぐに実行し、返される結果は-

a is less than 20
value of a is 10

実施例3

if句を使用してif-then-elseタイプのステートメントを作成することもできます。

main.lispという名前の新しいソースコードファイルを作成し、次のコードを入力します。

(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)

実行ボタンをクリックするか、Ctrl + Eを入力すると、LISPはすぐに実行し、返される結果は-

a is greater than 20
value of a is 100