This can change the sematics of code, if sometime later unreferenced lists will be replaced by referenced ones.
The manual says:
Trying to check this non-destructive behavior I've tried with unreferenced and context referenced lists.copy
syntax: (copy exp)
Make a copy from evaluating expression in exp. Some built-in functions are destructive, changing the original contents of a list, array or string they are working on. With copy their behavior can be made non-destructive.
Following code:
Code: Select all
"(setf c '()): " (setf c '())
(push "s_0" c) (push "s_1" c) (push "s_2" c)
"set-ref no copy......."
(set-ref (c 1) c 1) "c: " c
"set-ref with copy......."
(set-ref (c 1) (copy c) 2) "c: " c
""
"(define C:C): " (define C:C)
(push "s_0" C) (push "s_1" C) (push "s_2" C)
"set-ref no copy......."
(set-ref (C 1) C 1) "C:C : " C:C
"#### set-ref with copy......."
(set-ref (C 1) (copy C) 2) "C:C : " C:C
""
"trying to improve" ;; Update: code contains superfluous copy ops.
(define (alt_copy var)
(if (context? var)
(let (v (context var (sym (string var))))
(if (nil? v)
'()
(copy v)))
(copy var)))
""
"(setf c '()): " (setf c '())
(push "s_0" c) (push "s_1" c) (push "s_2" c)
"set-ref no copy......."
(set-ref (c 1) c 1) "c: " c
"set-ref with copy......."
(set-ref (c 1) (alt_copy c) 2) "c: " c
;;
"(define C:C): " (define C:C)
(push "s_0" C) (push "s_1" C) (push "s_2" C)
"set-ref no copy......."
(set-ref (C 1) C 1) "C:C : " C:C
"#### set-ref with copy......."
(set-ref (C 1) (alt_copy C) 2) "C:C : " C:C
;;
Code: Select all
;; copy
"#### set-ref with copy......."
("s_2" 2 "s_0")
"C:C : "
("s_2" 2 "s_0")
;; alt-copy
"#### set-ref with alt_copy......."
("s_2" 2 "s_0")
"C:C : "
("s_2" 1 "s_0")
Are there reasons not to have this as the default behavior?
Update after Lutz' reply
alt_copy contains superfluous copy operations and has other deficiencies: do not use it as it is! See later post for a better version.