CS-2510: Lecture Notes #1 Course: Fund. 2 Me: Matthew Ekstrand-Abueg What did we learn in Fund 1... (students) - Design Recipes * Structural recursion * Abstraction * Generative Recursion... Accumulators - Recursion (Structural? Generative?) ------------------------------------------------------ Refresher: Sum: [Listof Number] -> Number 1) Data... lists ;; A [Listof Number] is one of ;; - empty ;; - (cons Number [Listof Number]) 2) Contract/Purpose ;; sum : [Listof Number] -> Number ;; Sum the numbers in the list 3) Examples 4) Template (define (sum lon) (cond [(empty? lon) ...] [else ... (first lon) ... (rest lon) ... (sum (rest lon))])) ;; ** What does the recursion represent? ;; (in English) 5) Code! Case for Empty... Case for Cons... 6) Run Tests ("all X tests passed") ------------------------------------------------------- Administrivia Web: http://www.ccs.neu.edu/course/cs2510/ Book: htdp.org, PDF, Course Packs in the Book Store (if desired) Office: 472 WVH Exams: In Lecture: Thursday, October 6th Thursday, November 3rd 6pm-9pm: Thursday, December 1st *** CCIS Accounts!!! *** Lab accounts, HW Handin, grades Partners: 1 switch mid-semester... choose tomorrow in Lab Lab: Start tomorrow ------------------------------------------------------- Discussion: - Step through an example: * stack buildup, too much to remember (R to L) - How else can we do it? - Examples ;; reverser : [Listof Posn] -> [Listof Posn] ;; Reverse a list ;; farthest : [Listof Posn] -> Posn ;; Find farthest from (0,0) assuming non-empty - If necessary, motivate with total distance ;; dist : Posn Posn -> Number (define (dist p1 p2) (sqrt (sqr (- (posn-x p1) (posn-x p2))) (sqr (- (posn-y p1) (posn-y p2))))) ;; total-dist : [Listof Posn] -> Number ;; Sum the total route length from start to finish (define (total-dist lop) ...) * Need to keep the previous Posn * Need helper to enforce non-empty list ;; total-dist-help : Posn [Listof Posn] -> Number (define (total-dist-help p lop) ...) Better (sometimes): * Fold[r/l] * Map, Filter, BuildList