;; 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-reader.ss" "lang")((modname phone-book) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (define-struct pair (name num)) ;;An Entry is a (make-pair String Number) (define e1 (make-pair "bob" 123)) (define e2 (make-pair "alice" 456)) (define e3 (make-pair "carol" 789)) ;;A LOE [list of Entry] is one of ;; - empty ;; - (cons Entry LOE) (define pb (cons e1 (cons e2 (cons e3 empty)))) (define pb2 (cons e1 (cons e2 (cons e3 (cons e1 empty))))) ;;LOE String -> LOE ;;remove the contact with the given name from the phone-book (define (remove-e aloe str) (cond [(empty? aloe) empty] [(cons? aloe) (if (name-checker (first aloe) str) (remove-e (rest aloe) str) (cons (first aloe) (remove-e (rest aloe) str)))])) (check-expect (remove-e pb "bob") (cons e2 (cons e3 empty))) (check-expect (remove-e empty "bob") empty) (check-expect (remove-e pb "billy") pb) (check-expect (remove-e pb2 "bob") (cons e2 (cons e3 empty))) ;;Entry String -> Boolean ;;check if the name in the Entry matches the given string (define (name-checker an-e str) (string=? (pair-name an-e) str)) (check-expect (name-checker e1 "bob") true) (check-expect (name-checker e2 "bob") false)