0
(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?

sds
  • 58,617
  • 29
  • 161
  • 278
  • It's not about 'remove parenthesis'. Parentheses are only a thing in an external textual representation of a list. The operation is called 'flatten'. It takes a nested list and returns a new list with a flat list, which then contains all the atoms of the original list. – Rainer Joswig Mar 12 '20 at 20:12

1 Answers1

2

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.

sds
  • 58,617
  • 29
  • 161
  • 278