;; ***************************** ;; * CS 2510 Lecture 1 Notes * ;; * Review and Accumulators * ;; ***************************** ;;> Data definitions... ;; A [Listof Number] is one of: ;; - empty ;; - (cons Number [Listof Number]) ;;> Simple recursive function... ;; sum-list : [Listof Number] -> Number ;; Add all the numbers in the list (define (sum-list lon) (cond [(empty? lon) 0] [else (+ (first lon) ;; The sum of the rest of the numbers (sum-list (rest lon)))])) (check-expect (sum-list empty) 0) (check-expect (sum-list (list 1)) 1) (check-expect (sum-list (list 1 2 3)) 6) ;;> Simple Accumulator Style function ;; sum-acc : [Listof Number] -> Number ;; Add all the numbers in the list using accumulator style (define (sum-acc lon) (local [;; help : Number [Listof Number] -> Number ;; The ACC is the sum of the numbers seen so far. (define (help acc lon) (cond [(empty? lon) acc] [else (help (+ acc (first lon)) (rest lon))]))] (help 0 lon))) (check-expect (sum-acc empty) 0) (check-expect (sum-acc (list 1)) 1) (check-expect (sum-acc (list 1 2 3)) 6) ;;> More complex use of accumulators ;;> Problem: Given a list of points (Posns), compute the ;;> total length of a "trip" from the first point ;;> to the last point in the list. ;;> Helper ;; dist : Posn Posn -> Number ;; Compute the straight-line distance between two points (define (dist p1 p2) (sqrt (+ (sqr (- (posn-x p1) (posn-x p2))) (sqr (- (posn-y p1) (posn-y p2)))))) (check-expect (dist (make-posn 0 0) (make-posn 0 0)) 0) (check-expect (dist (make-posn 0 0) (make-posn 3 4)) 5) (check-expect (dist (make-posn 0 0) (make-posn 0 5)) 5) ;;> The top-level function... ;; trip : [Listof Posn] -> Number ;; Find the total length of a trip over the list of posns (define (trip lop) (cond [(empty? lop) 0] [else (trip-acc 0 (first lop) (rest lop))])) (check-expect (trip (list (make-posn 0 0) (make-posn 3 4))) 5) (check-expect (trip (list (make-posn 0 5) (make-posn 0 0))) 5) ;;> The helper with two accumulors ;; trip-acc : Number Posn [Listof Posn] -> Number ;; The total trip, after travelling D, from P, over the list ;; The d-ACC is the Distance travelled so far, the p-ACC is the ;; Last point visited, i.e., where we came from. (define (trip-acc d p lop) (cond [(empty? lop) d] [else (trip-acc ;; Update the first ACC with the distance from the ;; last point to this one (+ d (dist p (first lop))) ;; This point becomes the last visited Last (first lop) ;; Rest of the list (rest lop))])) (check-expect (trip-acc 0 (make-posn 0 0) (list (make-posn 3 4))) 5) (check-expect (trip-acc 45 (make-posn 0 0) (list (make-posn 3 4))) 50)