Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
LISP - Decision Making
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages:
LISP provides following types of decision making constructs. Click the following links to check their detail.
| Construct | Description |
|---|---|
| cond | This construct is used for used for checking multiple test-action clauses.It can be compared to the nested if statements in other programming languages. |
| if | The if construct has various forms. In simplest form it is followed by a test clause, a test action and some other consequent action(s). If the test clause evaluates to true, then the test action is executed otherwise, the consequent clause is evaluated. |
| when | In simplest form it is followed by a test clause, and a test action. If the test clause evaluates to true, then the test action is executed otherwise, the consequent clause is evaluated. |
| case | This construct implements multiple test-action clauses like the cond construct. However, it evaluates a key form and allows multiple action clauses based on the evaluation of that key form. |
The cond Construct in LISP
The cond construct in LISP is most commonly used to permit branching.
Syntax for cond is:
(cond (test1 action1) (test2 action2) ... (testn actionn))
Each clause within the cond statement consists of a conditional test and an action to be performed.
If the first test following cond, test1, is evaluated to be true, then the related action part, action1, is executed, its value is returned and the rest of the clauses are skipped over.
If test1 evaluates to be nil, then control moves to the second clause without executing action1, and the same process is followed.
If none of the test conditions are evaluated to be true, then the cond statement returns nil.
Example
Create a new source code file named main.lisp and type the following code in it:
(setq a 10) (cond ((> a 20) (format t "~% a is less than 20")) (t (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:
value of a is 10
Please note that the t in the second clause ensures that the last action is performed if none other would.
The if Construct
The if macro is followed by a test clause that evaluates to t or nil. If the test clause is evaluated to the t, then the action following the test clause is executed. If it is nil, then the next clause is evaluated.
Syntax for if :
(if (test-clause) (<action1) (action2))
Example 1
Create a new source code file named main.lisp and type the following code in it:
(setq a 10) (if (> a 20) (format t "~% a is less 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:
value of a is 10
Example 2
The if clause can be followed by an optional then clause:
Create a new source code file named main.lisp and type the following code in it:
(setq a 10) (if (> a 20) then (format t "~% a is less 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 less than 20 value of a is 10
Example 3
You can also create an if-then-else type statement using the if clause.
Create a new source code file named main.lisp and type the following code in it:
(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)
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
The 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 it 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
The case Construct
The case construct implements multiple test-action clauses like the cond construct. However, it evaluates a key form and allows multiple action clauses based on the evaluation of that key form.
The syntax for case macro is:
The template for CASE is:
(case (keyform) ((key1) (action1 action2 ...) ) ((key2) (action1 action2 ...) ) ... ((keyn) (action1 action2 ...) ))
Example
Create a new source code file named main.lispand type the following code in it:
(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")))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
Thursday




0 comments:
Post a Comment