How to do a while loop in LISP

26,009

Solution 1

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.

Solution 2

If you count down, you don't need WHILE+decrement.

Your loop goes from row - 1 down to -1. We can write it as a FOR loop. Here are two examples:

(loop for row-number from (1- row) downto -1
      collect (find-index row-number col)))

If you want to count down from row to 0 (here using downfrom ... to instead of from ... downto):

(loop for row-number downfrom row to 0
      collect (find-index row-number col)))
Share:
26,009

Related videos on Youtube

Mitchell McLaughlin
Author by

Mitchell McLaughlin

Updated on March 03, 2020

Comments

  • Mitchell McLaughlin
    Mitchell McLaughlin about 4 years

    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!!!