How to do a while loop in LISP

前端 未结 2 1682
忘了有多久
忘了有多久 2021-01-12 23:47

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

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

2条回答
  •  伪装坚强ぢ
    2021-01-13 00:27

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

提交回复
热议问题