;; 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 lect03c) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "world.ss" "teachpack" "htdp"))))) #| The steps of design recipe (taken from How to Design Programs). 1. Data Def'n 2. Examples of Data 3. Template for functions handling the data 4a Write function header b Write function comments c Write test cases d Write function body i. a cond line for each variant of union data ii. pull out fields for each line handling a struct iii. add natural-recursion call, if present. 5. run tests |# ;;Compare to the data defn for natnums: ; Data def'n: ; A natnum is: ; - 0, or ; - (add1 [natnum]) ;What would a template look like for natnums? ;Did our code for ! follow the template? Yes! ;;;; The robo-rally game: ; A simple robot game has robots follow instructions like "walk 2, ; turn left, walk 4, walk 6, turn right ...". We want a data ; definition to capture this.; A list-of-num-and-sym is - empty, or - (cons number list-of-num-and-sym) - (cons symbol list-of-num-and-sym) (cons 1 (cons 'left (cons 5 (cons 'right empty)))) ;Alternate syntax: (list 1 'left 5 'right) ; Make sure to develop a cond with three clauses in cond, not 2 -- ;; total-steps : list-of-num-and-sym -> number ;; produces the sum of all the numbers in the list #| Trees. abe mona jackie homer marge bart (define abe (make-person "Abe" 1920 'blue 'unknown 'unknown)) (define mona (make-person "Mona" 1929 'brown 'unknown 'unkown)) (define jackie (make-person "Jackie" 1926 'brown 'unknown 'unknown)) (define marge (make-person "Marge" 1956 'blue jackie 'unknown)) (define homer (make-person "Homer" 1955 'brown mona abe)) (define bart (make-person "Bart" 1979 'brown homer marge)) contains-name? count-oldies find-blue-eyes find-blue-eyed-orphans find-oldies .... generalized find! find-long-names find-named-for-ancestor find-time-travelers: somebody whose has an ancestor whose birth-year is later than their own! |# #| Quote. Sexpr. (see GlobalPreferences.plist.orig build-list write filter (passing in a list) map, filter, fold(reduce); writing loop; Accumulator versions: ; sum-accum ; reverse : (-> (list-of any) (list-of any)) This follows from the template, although we need a helper: ; add-at-end : (-> (list-of animal) animal (list-of animal)) ; Given a list of animals and another animal, ; Return a list containing all the listed ; (define (add-at-end zoo newguy) ...) What is running time? Memory usage? Accumulators; tail-recursion. |#