;; 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-advanced-reader.ss" "lang")((modname |29.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;vector-sum : vector -> number ;Given avector, sum all its entries. (define (vector-sum avector) (local ((define (vector-sum-aux i) (cond [(zero? i) 0] [else (+ (vector-ref avector (sub1 i)) (vector-sum-aux (sub1 i)))]))) (vector-sum-aux (vector-length avector)))) ;vector-sum-aux : vector N -> number ;Given avector and i, sum up the numbers within a vector in [0,i) starting from i-1 to 0. ;vector-sum-lr : vector -> number ;Given avector, sums the vector from left to right (like English-reading humans would do). (define (vector-sum-lr avector) (local ((define avec-length (vector-length avector)) (define (vector-sum-lr-aux i) (cond [(= avec-length i) 0] [else (+ (vector-ref avector i) (vector-sum-lr-aux (add1 i)))]))) (vector-sum-lr-aux 0))) ;vector-sum-lr-aux : vector N -> number ;Given avector and i, sum up the elements within the vector from [0,i) from left to right. (define vector1 (vector 5 6 7 8)) (equal? (vector-sum vector1) 26) (equal? (vector-sum vector1) (vector-sum-lr vector1))