-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.ss
executable file
·65 lines (50 loc) · 2.25 KB
/
dict.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(load "lists.ss")
(define (first list)
(cond ((empty-list? list) empty-list)
(else (car list))))
(define (first-in list) (first list))
(define (rest-of list) (cdr list))
(define (next-in list) (rest-of list))
(define (make-pair key value) (key value))
(define (key pair) (first-in pair))
(define (value pair)
(cond ((empty-list? pair) empty-list)
(else (first (rest-of pair)))))
(define (first-pair-in list) (first-in list))
(define (first-pair-key-in dict) (key (first-pair-in dict)))
(define (first-pair-value-in dict) (value (first-pair-in dict)))
(define (next-pair-in list) (first (next-in list)))
(define (next-pair-key-in dict) (key (next-pair-in dict)))
(define (next-pair-value-in dict) (value (next-pair-in dict)))
(define (make-dict) (list "*dict*"))
(define (dict-add dict key value)
(cond ((empty-list? (next-in dict)) (set-cdr! dict (list (list key value))))
(else (dict-add (rest-of dict) key value))))
(define (dict-add-many dict pair-list)
(cond ((empty-list? pair-list) empty-list)
(else (begin
(dict-add dict (first-pair-key-in pair-list) (first-pair-value-in pair-list))
(dict-add-many dict (rest-of pair-list))))))
(define (dict-make-many pair-list)
(let ((d (make-dict)))
(dict-add-many d pair-list)
d))
(define (dict-find-pair-in dict key)
(cond ((empty-list? (next-in dict)) empty-list)
((equal? (next-pair-key-in dict) key) (next-pair-in dict))
(else (dict-find-pair-in (rest-of dict) key))))
(define (dict-has-key? dict key)
(cond ((empty-list? (dict-find-pair-in dict key)) #f)
(else #t)))
(define (dict-for-each-in dict f)
(cond ((empty-list? (next-pair-in dict)) empty-list)
(else (f (next-pair-in dict))
(dict-for-each-in (rest-of dict) f))))
(define (dict-get-value dict key)
(value (dict-find-pair-in dict key)))
(define (dict-get-keys-from dict)
(cond ((empty-list? (next-in dict)) empty-list)
(else (cons (next-pair-key-in dict) (dict-get-keys-from (rest-of dict))))))
(define (dict-get-values-from dict)
(cond ((empty-list? (next-in dict)) empty-list)
(else (cons (next-pair-value-in dict) (dict-get-values-from (rest-of dict))))))