;; 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-beginner-abbr-reader.ss" "lang")((modname week4_m_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; a person is ; - (make-person number symbol) (define-struct person (ssn name)) ;; example people: (define Adam (make-person 1 'adam)) (define George (make-person 2 'george)) (define Jon (make-person 3 'jon)) ;; lookup : number database -> person or false ;; to see if a person is in the database (define (lookup ssn database) (cond [(empty? database) false] [(cons? database) (cond [(person-matches? ssn (first database)) (first database)] [else (lookup ssn (rest database))])])) ; imagine a sequence of 100 adds: ; (define db1 (add Jon empty)) ; (define db2 (add George db1)) ; ... ; (define db100 (add Frank db99)) ; imagine a sequence of 100 lookups: ; ...(lookup db100 ...) ; ...(lookup db100 ...) ; best case: lookup Jon, takes 2-3 steps 100 times =~ 300 steps ; worst case: lookup UNKNOWN, takes 2-3 steps 100*100 =~ 30,000 (check-expect (lookup 2 (list Adam George Jon)) George) (check-expect (lookup 2 empty) false) ; person-matches?: number person -> boolean ; says whether person has given ssn (define (person-matches? ssn person) (= ssn (person-ssn person))) (check-expect (person-matches? 2 George) true) (check-expect (person-matches? 1 Jon) false) ;; add : person database -> database ;; to add the person to the database (define (add person database) (cons person database)) ;; start : database ;; an empty database (define start empty) ; database: revision 0 ; a database is: ; - empty ; - (cons person database)