Opened 4 years ago

Closed 4 years ago

#1304 closed defect (notabug)

let-binding / closure problem with trees

Reported by: amattpearson Owned by:
Priority: normal Milestone:
Component: ANSI CL Compliance Version: 1.10
Keywords: Cc:

Description

Clozure Common Lisp Version 1.10-r16196 (DarwinX8664)

I just tried this example and it seems sub lists remain in new created closure, whereas they should be new values:

(defun closure-eg ()
  (let ((l1 '((a 0) (b 0))))
    (incf (second (assoc 'a l1)))
    l1))

run this function more than once, and the l1 binding is only shallow-recreated??

MY-PACKAGE> (closure-eg)
((A 1) (B 0))
MY-PACKAGE> (closure-eg)
((A 2) (B 0))
MY-PACKAGE> (closure-eg)
((A 3) (B 0))

the numbers in first list (A n) should always be 1, shouldn't they?

Change History (1)

comment:1 Changed 4 years ago by rme

  • Resolution set to notabug
  • Status changed from new to closed

You're modifying literal/quoted data, which is undefined behavior in CL. From http://www.lispworks.com/documentation/HyperSpec/Body/s_quote.htm:

The consequences are undefined if literal objects (including quoted objects) are destructively modified.

If you write

(defun closure-eg ()
  (let ((l1 (list (list 'a 0) (list 'b 0))))
    (incf (second (assoc 'a l1)))
    l1))

then you'll get the desired behavior because this is guaranteed to produce a fresh l1 every time.

Note: See TracTickets for help on using tickets.