Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused loop variables in Lisp

Sometime, I need to iterate $n$ times where $n$ is the number of elements in a list. Of course, I could write something like:

(loop for i from 0 below (list-length l)
      do something)

But I like to write rather:

(loop for i from 0
      for u in l ; unused 'u' variable
      do something)

where the for u in l part is merely intended to stop the loop after $n$ iterations. But then I don't use the u variable, and the interpreter keeps complaining. Is there some way to handle this style of coding? Should I avoid it and use list-length instead?

like image 777
Thomas Baruchel Avatar asked Oct 26 '25 10:10

Thomas Baruchel


1 Answers

(loop for i from 0
      for NIL in l
      do something)

NIL should do it.

The LOOP destructuring pattern can be empty and the rest list elements are ignored. Additionally: In a LOOP destructuring pattern, NIL indicates that the variable is not used.

like image 68
Rainer Joswig Avatar answered Oct 27 '25 23:10

Rainer Joswig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!