This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter) Recap: filter and map filter and map are Schemes loops filter : ( ! boolean) list[ ] ! list[ ] extract list of elts that satisfy a predicate map : ( ! ) list[ ] ! list[ ] applies function to all elts, returning list of results Recall sum ;; sum : list[num] ! num ;; adds up the elements of a list of numbers (define (sum alon) (cond [(empty? alon) 0] [(cons? alon) (+ (first alon) (sum (rest alon)))])) Sum also loops; how to write it with filter/map? [try it] filter/map dont work for sum Both return lists  sum returns a number Sum requires another kind of loop We derived filter by looking at two programs with similar structure and abstracting the common parts into a helper function sum and product ;; sum : list[num] ! num ;; adds elts of a list of nums (define (sum alon) (cond [(empty? alon) 0] [(cons? alon) (+ (first alon) (sum (rest alon)))])) ;; prod : list[num] ! num ;; multiplies list of nums (define (prod alon) (cond [(empty? alon) 1] [(cons? alon) (* (first alon) (prod (rest alon)))])) Where do these two programs differ? sum and product ;; sum : list[num] ! num ;; adds elts of a list of nums (define (sum alon) (cond [(empty? alon) ] [(cons? alon) ( + (first alon) (sum (rest alon)))])) ;; prod : list[num] ! num ;; multiplies list of nums (define (prod alon) (cond [(empty? alon) 1 ] [(cons? alon) ( * (first alon) (prod (rest alon)))])) Make the blue parts parameters to a new function [try it] The New Loop ;; newloop : ? num list[num] ! num (define (newloop combine base alon) (cond [(empty? alon) base] [(cons? alon) (combine (first alon) (newloop (rest alon)))])) Write sum and product using newloop [try it] The New Loop ;; newloop : ? num list[num] ! num (define (newloop combine base alon) (cond [(empty? alon) base] [(cons? alon) (combine (first alon) (newloop (rest alon)))])) ;; sum : list[num] ! num (define (sum alon) (newloop + 0 alon) ;; prod : list[num] ! num (define (prod alon) (newloop * 1 alon) The New Loop ;; newloop : ? num list[num] ! num (define (newloop combine base alon) (cond [(empty? alon) base] [(cons? alon) (combine (first alon) (newloop (rest alon)))])) Write length (of a list) using newloop [try it] base and alon arguments are easy but combine The New Loop ;; newloop : ? num list[num] ! num (define (newloop combine base alon) (cond [(empty? alon) base] [(cons? alon) (combine (first alon) (newloop (rest alon)))])) What is combines contract? [try it] ;; combine : (we see from its use that it takes two arguments) ! The New Loop ;; newloop : ? num list[num] ! num (define (newloop combine base alon) (cond [(empty? alon) base] [(cons? alon) (combine (first alon) (newloop (rest alon)))])) What is combines contract?...
View
Full
Document
This note was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.
 Fall '07
 Fisler

Click to edit the document details