创建可变长度的链表

时间:2011-01-18 17:02:26

标签: linked-list common-lisp

我有((a)(b)(f(x)))的列表。我想得到的是((a)(b)(f(x1))(a)(b)(f(x2))(a)(b)(f(x3))))的链表结构。也就是说,根据用户的要求重复附加列表,并且变量的值是chaning,以使其值彼此唯一。我如何在LISP中实现它?

1 个答案:

答案 0 :(得分:2)

? (let ((list '((a) (b) (f (x))))
        (n 3))
    (flet ((copier (l n)
             (setf l (copy-tree l))
             (let ((sym (first (second (third l)))))
               (setf (first (second (third l)))
                     (intern (format nil "~a~a"
                                     (symbol-name sym)
                                     n))))
             l))
      (loop for i from 1 upto n
        nconc (copier list i))))

((A) (B) (F (X1)) (A) (B) (F (X2)) (A) (B) (F (X3)))