Friday, 13 June 2014

LISP - Loops

Filled under:

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

LISP - Loops

There may be a situation, when you need to execute a block of code numbers of times. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
Loop constructs
LISP provides the following types of constructs to handle looping requirements. Click the following links to check their detail.
ConstructDescription
loopThe loop construct is the simplest form of iteration provided by LISP. In its simplest form It allows you to execute some statement(s) repeatedly until it finds a return statement.
loop forThe loop for construct allows you to implement a for-loop like iteration as most common in other languages.
doThe do construct is also used for performing iteration using LISP. It provides a structured form of iteration.
dotimesThe dotimes construct allows looping for some fixed number of iterations.
dolistThe dolist construct allows iteration through each element of a list.

The loop Construct

The loop construct is the simplest form of iteration provided by LISP. In its simplest form It allows you to execute some statement(s) repeatedly until it finds a return statement.
It has the following syntax:
(loop (s-expressions))
Example
Create a new source code file named main.lisp and type the following code in it:
(setq a 10)
(loop 
   (setq a (+ a 1))
   (write a)
   (terpri)
   (when (> a 17) (return a)))
When you execute the code, it returns the following result:
11
12
13
14
15
16
17
18
Please note that without the return statement, the loop macro would produce an infinite loop.

The loop for Construct

The loop for construct allows you to implement a for-loop like iteration as most common in other languages.
It allows you to
  • set up variables for iteration
  • specify expression(s) that will conditionally terminate the iteration
  • specify expression(s) for performing some job on each iteration
  • specify expression(s), and expressions for doing some job before exiting the loop
The for loop for construct follows several syntax:
(loop for loop-variable in <a list>
   do (action))
            
(loop for loop-variable from value1 to value2
   do (action))
Example 1
Create a new source code file named main.lisp and type the following code in it:
(loop for x in '(tom dick harry)
     do (format t " ~s" x)
)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
TOM DICK HARRY
Example 2
Create a new source code file named main.lisp and type the following code in it:
(loop for a from 10 to 20
     do (print a)
)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
Example 3
Create a new source code file named main.lisp and type the following code in it:
(loop for x from 1 to 20
  if(evenp x)
  do (print x)
)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
2 
4 
6 
8 
10 
12 
14 
16 
18 
20

The do Construct

The do construct is also used for performing iteration using LISP. It provides a structured form of iteration.
The syntax for do statement:
(do (variable1       value1       updated-value1)
    (variable2       value2      updated-value2)
    (variable3       value3       updated-value3)
    ...
(test     return-value)
(s-expressions))
The initial values of each variable is evaluated and bound to the respective variable. The updated value in each clause corresponds to an optional update statement that specifies how the values of the variables will be updated with each iteration.
After each iteration, the test is evaluated, and if it returns a non-nil or true, the return-value is evaluated and returned.
The last s-expression(s) is optional. If present, they are executed after every iteration, until the test value returns true.
Example
Create a new source code file named main.lisp and type the following code in it:
(do ((x 0 (+ 2 x))
    (y 20 ( - y 2)))
    ((= x y)(- x y))
    (format t "~% x = ~d  y = ~d" x y))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
x = 0  y = 20
x = 2  y = 18
x = 4  y = 16
x = 6  y = 14
x = 8  y = 12

The dotimes Construct

The dotimes construct allows looping for some fixed number of iterations.
For example,
Create a new source code file named main.lisp and type the following code in it:
(dotimes (n 11)
    (print n) (prin1 (* n n)))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

The dolist Construct

The dolist construct allows iteration through each element of a list.
For example,
Create a new source code file named main.lisp and type the following code in it:
(dolist (n '(1 2 3 4 5 6 7 8 9))
    (format t "~% Number: ~d Square: ~d" n (* n n)))
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
Number: 1 Square: 1
Number: 2 Square: 4
Number: 3 Square: 9
Number: 4 Square: 16
Number: 5 Square: 25
Number: 6 Square: 36
Number: 7 Square: 49
Number: 8 Square: 64
Number: 9 Square: 81
Gracefully Exiting From a Block
The block and return-from allows you to exit gracefully from any nested blocks in case of any error.
The block function allows you to create a named block with a body composed of zero or more statements. Syntax is:
(block block-name(
...
...
))
The return-from function takes a block name and an optional (the default is nil) return value.
The following example demonstrates this:
Example
Create a new source code file named main.lisp and type the following code in it:
(defun demo-function (flag)
    (print 'entering-outer-block)
    (block outer-block
      (print 'entering-inner-block)
      (print (block inner-block
               (if flag
                 (return-from outer-block 3)
                 (return-from inner-block 5))
               (print 'This-wil--not-be-printed)))
      (print 'left-inner-block)
      (print 'leaving-outer-block)
      t))
(demo-function t)
(terpri)
(demo-function nil)
When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is:
ENTERING-OUTER-BLOCK 
ENTERING-INNER-BLOCK 

ENTERING-OUTER-BLOCK 
ENTERING-INNER-BLOCK 
5 
LEFT-INNER-BLOCK 
LEAVING-OUTER-BLOCK 

0 comments: