CPSC 110 Lecture Notes - Lecture 25: Foreach Loop, American Broadcasting Company, Natural Number

217 views5 pages
Verified Note
1 Dec 2018
School
Course
Professor

Document Summary

(require 2htdp/image) (require spd/tags) (@signature (listof number) -> number) [else (+ (first lon) (sum1 (rest lon)))])) (@template use-abstract-fn) (define (sum2 lon) (foldr + 0 lon)) (@template (listof number) accumulator) (define (sum3 lon0) ;; acc is number: sum of numbers in lon0 seen so far. ;; (sumacc (list ) 14) (local [(define (fn-for-lon lon acc) (cond [(empty? lon) acc] [else (fn-for-lon (rest lon) (+ acc (first lon)))]))] (fn-for-lon lon0 0))) Note that sum3 is tail recursive (foldr is not). ;; today we introduce a 4th way - using for-each loops. ;; is rather like map except that it calls the function for effect rather. ;; than to do something with the value produced by the function. ;; here"s the template for designing with the for-each construct: (define (fn lox) ;; acc is : (local [(define acc )] (begin (for-each lox) ; can use a expression or locally defined function ( acc))))

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