;; 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-lambda-reader.ss" "lang")((modname rev-fold) (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) (foldl (lambda (fst sofar) (cons fst sofar)) '() l0)) #| stress tests |# (define (main n) (throw-away (time (rev (make-list n 0))))) (define (throw-away x) 10) (main 10) ;; SO FROM HERE: ;; ;; List reverse(List l0) { ;; for(l=l0, sofar = NULL; l!=NULL; l = l.rest) ;; sofar = cons(l.first,sofar); ;; } ;; This is not an in-place update. But if the interviewer really asks you ;; for that, you can derive it in the same way.