CPSC 110 Lecture 20: CPSC 110

137 views2 pages
Verified Note
9 Nov 2018
School
Course
Professor

Document Summary

Problem: (a) consider the following function that consumes a list of numbers and produces the sum of all the numbers in the list. Use the stepper to analyze the behavior of this function as the list gets larger and larger. (b) use an accumulator to design a tail-recursive version of sum. (@htdf sum) ;; produce sum of all elements of lon (check-expect (sum empty) 0) (check-expect (sum (list 2 4 5)) 11) (@template (listof number)) (define (sum lon) (cond [(empty? lon) 0] [else (+ (first lon) (sum (rest lon)))])) (@htdf sumacc) ;; produce sum of all elements of lon (check-expect (sumacc empty) 0) (check-expect (sumacc (list 2 4 5)) 11) (@template (listof number) accumulator) (define (sumacc lon0) ;; rsf is number; sum of numbers before (first lon) in lon0. ;; (sumacc (list 5) 6) (local [(define (sumacc lon rsf) (cond [(empty? lon) rsf] [else (sumacc (rest lon) (+ rsf (first lon)))]))] (sumacc lon0 0)))

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions