;; 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 data_abstraction_1) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ; a person is: ; - number ssn ; - symbol name (define-struct person (ssn name)) (define Adam (make-person 1 'adam)) (define George (make-person 2 'george)) (define Jon (make-person 3 'jon)) ; lookup : number database -> person or false ; returns person if ssn in database, false otherwise (define (lookup n db) (cond [(empty? db) false] [(cons? db) (cond [(person-matches? (first db) n) (first db)] [else (lookup n (rest db)) ])]) ) (check-expect (lookup 2 (list Adam George Jon)) George) (check-expect (lookup 2 empty) false) ;person-matches? : person ssn ; returns if person has ssn (define (person-matches? person ssn) (= (person-ssn person) ssn)) ; add : person database -> database ; adds person to database (define (add per db) (cons per db)) ; start : database ; an empty database (define start empty) ; a database is: ; - empty ; - (cons person database) ; template: ; (cond [(empty? db) ...] ; [(cons? db) ... (first db) ... ; (fun-for-db (rest db)) ...])