;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname lect30-hof-sharing) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; make-len-checker: natnum -> (string -> boolean) ; (define (make-len-checker len) (lambda (str) (= (string-length str) len))) ; Note: the type of `is-len-3?` is: string -> boolean (define is-len-3? (make-len-checker 3)) (check-expect (is-len-3? "hi") #f) (check-expect (is-len-3? "huh") #t) (check-expect (map is-len-3? (list "hi" "hmm" "hah" "rats")) (list false true true false)) (check-expect (map (make-len-checker 2) (list "hi" "hmm" "hah" "rats")) (list true false false false)) ; By the law-of-racket for student-languages, we substituted `len` away and ; returned a lambda-value with three embedded inside. ; But if mutation is wanted/needed, we need to keep `len` around inside the lambda... ; even though `len` was a local-variable for `make-len-checker`. ; Odd -- we keep a function's local-variable around even after the function has ended? ; ; Note that the variable `len` is in the "closure" of the function `is-len-3?`. ; We'll handle closures (w/ mutation) in our interpreter project. ; Note: it's reasonable to have two open-parens in a row: ; ((make-len-checker 3) "wow") ; ; The same law-of-racket we saw earlier still applies: ; "evaluate each expression inside the function-application; ; the first had better evaluate to a function-value!". ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; data def'n: ; an anc-tree (ancestor tree) is: ; - 'unknown ; - (make-child [string] [int] [symbol] [anc-tree] [anc-tree]) (define-struct child (name yob eye ma pa)) ; examples of AncTrees: ; examples of data of anc-tree: 'unknown (define abe (make-child "Abe" 1920 'blue 'unknown 'unknown)) (define mona (make-child "Mona" 1929 'brown 'unknown 'unknown)) (make-child "homer" 1955 'brown (make-child "Mona" 1929 'brown 'unknown 'unkown) (make-child "Abe" 1920 'blue 'unknown 'unknown)) (define (func-for-ancTree tr) (cond [(symbol? tr) ...] [(child? tr) (...(child-name tr) ...(child-yob tr) ...(child-eye tr) ...(func-for-ancTree (child-ma tr)) ...(func-for-ancTree (child-pa tr)) )])) (define jackie (make-child "Jackie" 1926 'brown 'unknown 'unknown)) (define marge (make-child "Marge" 1956 'blue jackie 'unknown)) (define homer (make-child "Homer" 1955 'brown mona abe)) (define bart (make-child "Bart" 1979 'brown marge homer)) ;; fun-for-anc-tree : anc-tree --> ??? ;; ;(define (fun-for-anc-tree anc) ; (cond [(unknown? anc) ...] ; [(child? anc) ...(child-name anc) ; ...(child-yob anc) ; ...(child-eye anc) ; ...(fun-for-anc-tree (child-ma anc)) ; ...(fun-for-anc-tree (child-pa anc))]) ; all-names : anc-tree -> (listof string) ; Return a list of all the names in a tree. ; ;; (define (all-names anc) (cond [(unknown? anc) empty] [(child? anc) (append (all-names (child-ma anc)) (list (child-name anc)) (all-names (child-pa anc)))])) ; A note about sharing and references, between the trees `homer`, `bart`, `abe` etc. ; W/o mutation, it's impossible to know if there is sharing, or whether the struct ; contains the entire subtree inside each field. (It's a religious argument: ; can never be proven or make any visible difference to a check-expect.) ; ; HOWEVER, if we have mutation (which advanced-student and full racket have, ; even though we won't use them for this class), then you *could* tell: ; you could modify `marge`s yob, and then see if `(child-yob (child-ma bart))` ; has changed or not. ; all-childs : anc-tree -> (listof child) ; Return a list of all the childs in a tree. ; ;; (define (all-childs anc) (cond [(unknown? anc) empty] [(child? anc) (append (all-childs (child-ma anc)) (list anc) (all-childs (child-pa anc)))])) (check-expect (all-childs 'unknown) empty) (check-expect (all-childs abe) (list abe)) (check-expect (all-childs homer) (list mona homer abe)) (check-expect (all-childs bart) (list jackie marge bart mona homer abe))