;; 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-intermediate-lambda-reader.ss" "lang")((modname dictionary-examples) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) "using association lists (or 'a-lists'):" (define alist1 '(("three" 3) ("four" 4) ("five" 5))) ; retrieve from an a-list: (assoc "four" alist1) ;-> (list "four" 4) (which is also writable as '("four" 4)) (assoc "seven" alist1) ;-> #false ; Add to an a-list (define alist2 (cons (list "four" 44) alist1)) ; just cons a new entry on the front (second (assoc "four" alist2)) ;-> 44 "" "using hash tables (immutable):" ; From the student-languages, import the hash-table functions with: (require "student-extras.rkt") (define hash-table1 #hash(("three" . 3) ("four" . 4) ("five" . 5))) ; retrieve from a hash: (hash-ref hash-table1 "four") ;-> 4 ;(hash-ref hash-table1 "seven") ;-> ERROR, no such key (hash-has-key? hash-table1 "seven") ;-> #false (hash-ref hash-table1 "seven" 'my-own-default) ;-> 'my-own-default ; Add to a hash-table: (define hash-table2 (hash-set hash-table1 "four" 44)) (hash-ref hash-table2 "four") ;-> 44 #| @license: CC0 @author: ibarland@radford.edu @version: 2020-apr-27 |#