Lisp 中未使用的循环变量

Tho*_*hel 4 lisp loops common-lisp unused-variables

有时,我需要迭代 $n$ 次,其中 $n$ 是列表中的元素数量。当然,我可以写这样的东西:

(loop for i from 0 below (list-length l)
      do something)
Run Code Online (Sandbox Code Playgroud)

但我更喜欢这样写:

(loop for i from 0
      for u in l ; unused 'u' variable
      do something)
Run Code Online (Sandbox Code Playgroud)

其中该for u in l部分仅用于在 $n$ 迭代后停止循环。但后来我不使用该u变量,解释器不断抱怨。有什么方法可以处理这种编码风格吗?我应该避免它并使用它list-length吗?

Rai*_*wig 5

(loop for i from 0
      for NIL in l
      do something)
Run Code Online (Sandbox Code Playgroud)

NIL 应该这样做。

LOOP 解构模式可以为空,其余列表元素将被忽略。另外:在 LOOP 解构模式中,NIL 表示未使用该变量。

  • 谢谢你;这确实是我所要求的!我可以通过引用其他语言中的“可抛出变量”(Python 中的下划线)来更好地解释我的问题,但你完全理解了这一点。 (2认同)