;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-reader.ss" "lang")((modname rev-accu) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A LON is either empty or (cons Number LON). ;; LON -> LON ;; reverse the order of the numbers on the list ; (check-expect (rev (list 1 2 3)) (list 3 2 1)) (define (rev l0) (local (;; LON LON -> LON ;; so-far: the reverse of the list between l0 and l ;; l0 = '(1 2 3 4) ;; l = '(3 4) ;; so-far = '(2 1) (define (rev-with-accu l so-far) (cond [(empty? l) so-far] [else (rev-with-accu (rest l) (cons (first l) so-far))]))) (rev-with-accu l0 empty))) #| stress tests (define (main n) (throw-away (time (rev (make-list n 0))))) (define (throw-away x) 10) (main 10) |# (rev '(1 2 3))