如何模拟嵌套循环中的收集

问题描述

我想创建一个(a . b)对与1 < a < b <= nn对的列表,例如n = 5

((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))

(对的顺序并不重要。)

我想出了代码

(defun create-pairs (upper-bound)
  (loop for i from 3 to upper-bound
        for j from 2 to (1- upper-bound)
          collecting (cons j i)))

但这不能满足我的要求

* (create-pairs 5)
((2 . 3) (3 . 4) (4 . 5))

因为循环是同时增加的。

因此我尝试了

(defun create-pairs (upper-bound)
  (loop for i from 3 to upper-bound do
    (loop for j from 2 to (1- upper-bound)
      collecting (cons j i))))

结果:

* (create-pairs 5)

NIL

我不记得在哪里了,但我读到不可能像第二次尝试那样在结构中使用collecting

那么我如何获得想要的结果?不可能用loop for解决吗?

解决方法

您快到了-您只需accumulate内部loop的结果:

(defun create-pairs (upper-bound)
  (loop for i from 3 to upper-bound nconc
    (loop for j from 2 below i
      collect (cons j i))))
(create-pairs 5)
==> ((2 . 3) (2 . 4) (3 . 4) (2 . 5) (3 . 5) (4 . 5))