;; 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]) #;(define (fun-for-natnum n) (cond [(zero? n) ...] [(positive? n) ...(- n 1)...])) ;What would a template look like for natnums? ;Did our code for ! follow the template? Yes! (define (! n) (cond [(= n 0) 1] [(> n 0) (* n (! (- n 1))) ])) ;;;; 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.; ; Data Def'n: ; A list-of-num-and-sym is ; - empty, or ; - (cons number list-of-num-and-sym) ; - (cons symbol list-of-num-and-sym) (define (fun-for-losan moves) (cond [(empty? moves) ...] [(number? (first moves)) ...(first moves) ... (fun-for-losan (rest moves))] [(symbol? (first moves)) ...(first moves) ... (fun-for-losan (rest moves))])) (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 (define (total-steps moves) (cond [(empty? moves) 0] [(number? (first moves)) (+ (first moves) (total-steps (rest moves)))] [(symbol? (first moves)) (total-steps (rest moves))])) ; Data Def'n: ; A treenode is: ; - 'unknown ; - (make-person string num symbol treenode treenode) (define-struct person (name year eye ma pa)) (define abe (make-person "Abe" 1920 'blue 'unknown 'unknown)) (define mona (make-person "Mona" 1929 'brown 'unknown 'unkown)) (make-person "homer" 1955 'brown (make-person "Mona" 1929 'brown 'unknown 'unkown) (make-person "Abe" 1920 'blue 'unknown 'unknown)) (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)) (define (fun-for-tnode a-ft) (cond [(symbol? a-ft) ...] [(person? a-ft) (person-name a-ft) ... (person-year a-ft) ... (person-eye a-ft) ... (fun-for-tnode (person-ma a-ft)) ... (fun-for-tnode (person-pa a-ft)) ... ])) (define (contains? a-ft target) (cond [(symbol? a-ft) false] [(person? a-ft) (or (string=? target (person-name a-ft)) (contains? (person-ma a-ft) target) (contains? (person-pa a-ft) target)) ])) (define (count a-ft) (cond [(symbol? a-ft) 0] [(person? a-ft) (+ 1 (count (person-ma a-ft)) (count (person-pa a-ft))) ])) #| Possible next exercises: 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! |#