Reputation: 13
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
Reputation: 114481
delete
is allowed to destroy/mutate/reuse parts of the original list to return the result, this has two implications:
(setf set1 (delete ...))
(setf set1 (list 'Nr1 'Nr2 'Nr3))
insteadUpvotes: 3
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