Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a while loop in LISP

I cannot get a simple while loop to work in lisp!

(loop (while (row >= 0))
      setf(row (- row 1))
      (collect (findIndex row col))

while row is more or equal to 0 i want to decrement row and collect the result given by findIndex method. Suppose the col is given.

Thanks!!!

like image 494
Mitchell McLaughlin Avatar asked Mar 02 '16 05:03

Mitchell McLaughlin


People also ask

How do you make a while loop?

Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.

Are there loops in Lisp?

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. The loop for construct allows you to implement a for-loop like iteration as most common in other languages.

Do while () loop is?

A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. Some languages may use a different naming convention for this type of loop.


1 Answers

The correct form of the loop is the following:

(loop while (>= row 0) 
  do (setf row (- row 1))           ; or better: do (decf row)
  collect (findIndex row col))

For a detailed description of the loop syntax, see the manual.

like image 140
Renzo Avatar answered Nov 15 '22 10:11

Renzo