LISP - When Construct



The when macro is followed by a test clause that evaluates to t or nil. If the test clause is evaluated to nil, then no form is evaluated and nil is returned, however the test result is t, then the action following the test clause is executed.

Syntax for when macro −

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

Example

Create a new source code file named main.lisp and type the following code in it.

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

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

a is greater than 20
value of a is 100 
lisp_decisions.htm
Advertisements