slowman01
slowman01

Reputation: 13

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?

Upvotes: 0

Views: 67

Answers (2)

6502
6502

Reputation: 114481

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

Upvotes: 3

pjstirling
pjstirling

Reputation: 301

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

Upvotes: 1

Related Questions