;; ;; length, copy, and append ... in class ;; (define things1 (list 1 2 3 4 5)) (define things2 (list 10 20 30 40 50)) (define (length stuff) ;; O(n) time ;; O(n) space (if (null? stuff) 0 (+ 1 (length (cdr stuff))))) (printf "(length things1 is ~a.\n" (length things1)) (define (length2 stuff) ;; O(n) time ;; O(1) space (define (length2-iter stuff total) (if (null? stuff) total (length2-iter (cdr stuff) (+ 1 total)))) (length2-iter stuff 0)) (printf "(length2 things2 is ~a.\n" (length2 things2)) (define (copy items) ;; return a copy of a list (if (null? items) '() (cons (car items) (copy (cdr items))))) (printf "(copy things1) is ~a.\n" (copy things1)) (define (append stuff1 stuff2) ;; make a new list with one list after the other list (if (null? stuff1) (copy stuff2) (cons (car stuff1) (append (cdr stuff1) stuff2)))) (printf "(append things1 things2) is ~a.\n" (append things1 things2))