;; 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)) ; database: revision 0 ; a database is either: ; - empty ; - (cons person database) ;; lookup : number database -> person or false ;; to see if a person is in the database (define (lookup n db) (cond [(empty? db) false] [else (cond [(person-matches n (first db)) (first db)] [else (lookup n (rest db))])]) ) (check-expect (lookup 2 (list Adam Jon)) false) (check-expect (lookup 3 (list Adam Jon)) Jon) (check-expect (lookup 1 (list Adam Jon)) Adam) ; person-matches : number person -> boolean ; returns whether person has given ssn (define (person-matches n per) (= n (person-ssn per))) (check-expect (person-matches 1 Adam) true) (check-expect (person-matches 2 Adam) false) ;; add : person database -> database ;; to add the person to the database (define (add person database) (cons person database)) (check-expect (add Adam empty) (cons Adam empty)) (check-expect (add Adam (list Jon)) (list Adam Jon)) ;; start : database ;; an empty database (define start empty) ;performance: ; 100 adds? ; 1 + 2 + 3 + 4 +.... ; 100 lookups? in database with 100 things ; best case: 1 + 1 +... + 1 = 100 ; worst case: 101 + 101 + ... + 101 = 10100 ; n lookups in database with n things ; worst case: about n*n