quiz: template for weight-to-frobboz function. see weight data defn below. Your template is a mechanism to drive an input analysis; cheap way to shape your thinking. 14 days to midterm; no final ;; Unions with self-referential definitions -- RECURSION! ;; Matrioshka dolls ;; what do you need to represent the innermost doll? ;; ATOMIC data (define-struct shell (contents)) ;; VERSION 1: ;; A RD (Russian Doll) is one of: ;; -- 'solid ;; -- (make-shell 'solid) ;; ... ;; -- (make-shell (make-shell ..... 'solid)) ;; 4-layer RD set ;; RD -> number ;; count the number of layers of a Russian Doll (define (rd-layers rd) (cond ((symbol? rd) ...) ...)) ;; Our set is 10 layers. ;; nested ivory spheres at the gugong bowuguan -- 43 layers ;; thousand layers #| ------------------------------------------------------------------------ QUIZ: (define-struct grams (kilo)) ;; A Weight (weight measurement) is one of: ;; -- Number ;; -- (make-grams Number) ;; interp: a plain number represents American pounds, ;; and (make-grams n) represents kilograms. Create a template for the function measured that consumes a Weight, i.e., ;; measured : Weight -> Boolean (define (measured w) |# ;; VERSION 2: ;; +-------------------------------+ ;; | | ;; v | ;; A RD (Russian Doll) is one of: | ;; --- 'solid | ;; --- (make-shell RD-body) x------+ ;; HEY! Circular definition! Does this work? ;; Well... can we build examples? ;; examples of data 'solid (make-shell 'solid) (make-shell (make-shell 'solid)) (make-shell (make-shell (make-shell 'solid))) ;; Do the doll-level function by the numbers. ;; template: ;; unshell : RD -> Number #; (define (doll-level rd) (cond [(symbol? rd) ...] [(shell? rd) ... (shell-contents rd) ...])) ;; where is the BACK-ARROW??? ;; Use name of fun for backarrow. This is recursion! #; (define (doll-level rd) (cond [(symbol? rd) ...] [(shell? rd) ... (doll-level (shell-contents rd)) ...])) ;; now there is a backarrow in the function definition exactly where ;; there is a backarrow in the data definition. ;; examples of function behavior ;; 'solid |-----> 0 ;; (make-shell 'solid) |-----> 1 ;; (make-shell (make-shell 'solid)) |-----> 2 ;; (make-shell (make-shell (make-shell 'solid))) |-----> 3 (define (doll-level rd) (cond [(symbol? rd) 0] [(shell? rd) (+ (doll-level (shell-contents rd)) 1)])) (equal? (doll-level 'solid) 0) (equal? (doll-level (make-shell 'solid)) 1) (equal? (doll-level (make-shell (make-shell 'solid))) 2) (equal? (doll-level (make-shell (make-shell (make-shell 'solid)))) 3) ;;; A New Structure: ;;; constructor: cons -- creates a 2-slot structure called ;;; 'first' and 'rest' slot ;;; accessor: first, rest -- extract the first and the rest slot ;;; predicate: cons? -- is the given 'thing' (value) a cons ;;; structure ;;; A New Unique Value: ;;; empty -- it's a constant, just like 5 or true ;;; empty? -- it's different from every other value ;;; in the world ;; +----------------------------------------- ;; | ;; V ;; A LOS (List of Strings) is one of: ;; -- empty ;; -- (cons String LOS) x----------------------- ;; another cycle!!!! ;; let's make examples: empty (cons "hello" empty) (cons "world" (cons "hello" empty)) ;; .... ;; Is this one: (cons "hello" (cons "world" empty)) ;; show how to use a data definition to check whether some value ;; "belongs" ;; LOS -> Number ;; count the number of strings in the given LOS #; (define (count l) (cond [(empty? l) ...] [(conns? l) ... (first l) ... (count (rest l)) ...])) ;; finish ;; LOS -> Number ;; count the number of chars in the given LOS ;; same template! ;; work!