Search code examples
common-lispallegro-cl

strange behaviour of (delete .. in Allegro Lisp


I try to remove random elements from a list until the list is empty. My environment is Allegro Lisp, free edition.

(setq set1 '(Nr1 Nr2 Nr3))  
(delete (nth (random (length set1)) set1) set1)  

After some repetitions, the output of sequential calls to delete (and the content of set1) start to look like this:

(NR1 NR2 NR3)  
(NR2 NR3)  
(NR1 NR2)  
(NR1)  
NIL

That is, sometimes items are not deleted, sometimes deleted items reappear. If I use remove there is no such problem.

Is this a bug or do I miss something about delete?


Solution

  • When delete is asked to remove the first element of a list, it is allowed to simply return (cdr list), you need to (setf set1 (delete (nth (random (length set1)) set1)))