;; An entry holds a key/value pair ;; for use in Dictionaries. ;; (define-struct entry (key value)) ;; have-key? key entry --> boolean ;; Returns whether the key is the same as that of the entry. (define (have-key? key entry) (equal? key (entry-key entry))) ;; A Dictionary is a structure with two "methods" (fields which are functions): ;; add-fn! and lookup-fn are functions which signatures below. ;; (define-struct dict (add-fn! lookup-fn)) ;; add-fn!: ANY ANY --> ?? ;; SIDE-EFFECT: change the contents. ;; We return (the updated) contents, primarily for debugging purposes. ;; lookup: key --> entry or false ;; Does the key occur inside "contents"? ;; If so, return that entry, else return the sentinel false. ;;;;;;;;;;;;;;;;;; (define dict-Factory (local [(define dict-count 0)] (lambda () (local [;; a local list-of-entries, ;; used only by the dictionary we're creating now: ;; (define contents empty) ;; All contracts as per Dictionary above. (define (add! k val) (begin (set! contents (cons (make-entry k val) contents)) contents)) (define (lookup key) (local [;; helper: list-of-entries --> entry or false ;; Does the key occur inside "entries"? ;; If so, return that entry, else return false. ;; (define (helper entries) (cond [(empty? entries) false] [(cons? entries) (cond [(have-key? key (first entries)) (first entries)] [else (helper (rest entries))])]))] (helper contents))) ] (begin (set! dict-count (add1 dict-count)) (make-dict add! lookup)))))) (define (dict-n-Factory num-keys) (local [; Our local datastore, for numeric keys: a vector. (define data-smallnums (make-vector num-keys false)) ; Our local datastore, for (define data-other (dict-Factory)) ;; in-range?: key --> boolean ;; Is k a valid key for the vector? ;; (define (in-range? k) (and (integer? k) (<= 0 k (sub1 num-keys)))) ;; All contracts as per Dictionary above. ;; We proceed by using the vector if possible, ;; else just forward the call to our other robust dict implementation. ;; add!: ANY, ANY --> vector-of-entry or list-of-entry ;; Again, the return value is for debugging mroe than anything. ;; (define (add! k v) (cond [(in-range? k) (begin (vector-set! data-smallnums k v) data-smallnums)] [else ((dict-add-fn! data-other) k v)])) (define (lookup k) (cond [(in-range? k) (vector-ref data-smallnums k)] ; Note how we initialized the vector to contain false. [else ((dict-lookup-fn data-other) k)])) ] (make-dict add! lookup))) ;;;;;; Tests ; Okay, actually both german and french nouns ; *should* be represented by a structure which includes a gender field. (define english->german (dict-n-Factory 10)) (boolean=? false ((dict-lookup-fn english->german) 'health)) (boolean=? false ((dict-lookup-fn english->german) 1)) (boolean=? false ((dict-lookup-fn english->german) 11)) ((dict-add-fn! english->german) 'health 'gesundheit) ((dict-add-fn! english->german) 1 'eins) ((dict-add-fn! english->german) 11 'elf) ((dict-lookup-fn english->german) 'health) ((dict-lookup-fn english->german) 1) ((dict-lookup-fn english->german) 11) ((dict-add-fn! english->german) 'science 'wissenschaft) ((dict-lookup-fn english->german) 'health) (define english->french (dict-n-Factory 10000)) (boolean=? false ((dict-lookup-fn english->french) 'health)) ((dict-add-fn! english->french) 'health 'sante) ((dict-add-fn! english->french) 'computer 'ordinateur) ((dict-add-fn! english->french) 'weekend 'weekend) ((dict-lookup-fn english->french) 'health) ((dict-add-fn! english->german) 'computer-science 'informatik) ((dict-lookup-fn english->german) 'science) ((dict-lookup-fn english->german) 'health) (boolean=? false ((dict-lookup-fn english->german) 'gymnasium))