Consider these two similar procedures:
(define (append-internal-cons obj)
(let ((local-var (cons 1 '()) ))
(append! local-var (list obj))
local-var))
(define (append-internal-no-cons obj)
(let ((local-var '(1) ))
(append! local-var (list obj))
local-var))
Given these two helper procedures:
(define (append! x y)
(set-cdr! (last-pair x) y)
x)
(define (last-pair ls)
(if (null? (cdr ls))
ls
(last-pair (cdr ls))))
Calling the first one three times yields expected results: a list containing 1
and whatever obj
we passed in.
(append-internal-cons 2) ;; (1 2)
(append-internal-cons 3) ;; (1 3)
(append-internal-cons 4) ;; (1 4)
I would expect calling the second version three times to yield the same results, but it doesn't.
(append-internal-no-cons 2) ;; (1 2)
(append-internal-no-cons 3) ;; (1 2 3)
(append-internal-no-cons 4) ;; (1 2 3 4)
Shouldn't the behavior of the two procedures be the same?