strange behaviour of (delete .. in Allegro Lisp

67 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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)))

2
On

delete is allowed to destroy/mutate/reuse parts of the original list to return the result, this has two implications:

  1. You should use (setf set1 (delete ...))
  2. Mutating a quoted list is not a good idea, just use (setf set1 (list 'Nr1 'Nr2 'Nr3)) instead