JunDeveloper
JunDeveloper

Reputation: 1

how to remove parenthesis from sublists within a list?

(defun ppl (list)
  (loop for x in list
    collect (cons x '(ppl))))
(ppl '(1 2 3))
=> ((1 ppl) (2 ppl) (3 ppl))

While still inside ppl, how do I remove the parenthesis so that the result becomes

=> (1 ppl 2 ppl 3 ppl)

I understand that my code fundamentally creates a list of sublists. Could use some help on flattening out the list. Perhaps if I could in some way get the list that collect returns?

Upvotes: 0

Views: 112

Answers (1)

sds
sds

Reputation: 60014

Use nconc instead of collect:

(defun ppl (list)
  (loop for x in list
    nconc (list x 'ppl)))
(ppl '(1 2 3))
==> (1 PPL 2 PPL 3 PPL)

Please note that I replaced (cons x '(ppl)) with (list x 'ppl)) to avoid possible reuse of literal (ppl) which nconc could turn into circular lists. If you insist on having quoted literals in your code (bad idea!), use append instead of nconc.

Upvotes: 2

Related Questions