(define (list-copy list) (define (list-copy-loop l tail last) (cond ((null? l) tail) (else (set-cdr! last (cons (car l) nil)) (list-copy-loop (cdr l) tail (cdr last))))) (if (pair? list) (let ((first (cons (car list) nil))) (list-copy-loop (cdr list) first first)))) (define (vector-copy v) (define (vector-copy-loop u n m) (cond ((< n m) (vector-set! u n (vector-ref v n)) (vector-copy-loop u (+ n 1) m)) (else u))) (let ((l (vector-length v))) (vector-copy-loop (make-vector l) 0 l))) (define (my-copy tree) (cond ((atom? tree) tree) (cons (copy (car tree)) (copy (cdr tree))))) (define (tree-copy tree) (define stack-of-cdrs '()) (define (tree-copy-loop l) (cond ((pair? (car l)) (if (pair? (cdr l)) (set! stack-of-cdrs (cons l stack-of-cdrs))) (set-car! l (cons (caar l) (cdar l))) (tree-copy-loop (car l))) ((pair? (cdr l)) (set-cdr! l (cons (cadr l) (cddr l))) (tree-copy-loop (cdr l))) ((pair? stack-of-cdrs) (let ((i stack-of-cdrs) (j (car stack-of-cdrs))) (set! stack-of-cdrs (cdr stack-of-cdrs)) (set-car! i (cadr j)) (set-cdr! i (cddr j)) (set-cdr! j i) (tree-copy-loop i))))) (if (pair? tree) (let ((n (cons (car tree) (cdr tree)))) (tree-copy-loop n) n) tree))