|
8 | 8 |
|
9 | 9 | ;; Throw in a mediocre flatten definition |
10 | 10 | (define (flatten lst) |
11 | | - (cond ((null? lst) empty) |
12 | | - ((list? lst) |
13 | | - (append (flatten (car lst)) (flatten (cdr lst)))) |
14 | | - (else (list lst)))) |
| 11 | + (cond |
| 12 | + [(null? lst) empty] |
| 13 | + [(list? lst) (append (flatten (car lst)) (flatten (cdr lst)))] |
| 14 | + [else (list lst)])) |
15 | 15 |
|
16 | 16 | ;; contract: (listof char?) (listof tries?) integer? -> (listof trie?) |
17 | 17 | (define (create-children char-list lst prefix-chars) |
18 | | - (cond [(= (length char-list) 1) |
19 | | - (handle-last-letter char-list lst prefix-chars)] |
20 | | - [else ;; you are in the middle of the word |
21 | | - (handle-intern-letter char-list lst prefix-chars)])) |
| 18 | + (cond |
| 19 | + [(= (length char-list) 1) (handle-last-letter char-list lst prefix-chars)] |
| 20 | + ;; you are in the middle of the word |
| 21 | + [else (handle-intern-letter char-list lst prefix-chars)])) |
22 | 22 |
|
23 | 23 | ;; contract: (listof char?) (listof trie?) integer? -> (listof trie?) |
24 | 24 | (define (handle-last-letter char-list lst prefix-chars) |
25 | 25 | (define char (first char-list)) |
26 | | - ; (define next-prefix (append prefix-chars (list char))) |
| 26 | + ; (define next-prefix (append prefix-chars (list char))) |
27 | 27 | (define next-prefix (push-back prefix-chars char)) |
28 | | - (cond [(empty? lst) ;; children are empty, return list of empty children |
29 | | - (list (trie char empty #t next-prefix))] |
30 | | - [(< char (trie-char (first lst))) ;; less than, put it to the left |
31 | | - (cons (trie char empty #t next-prefix) lst)] |
32 | | - [(= char (trie-char (first lst))) ;; equal, step down a level |
33 | | - (cons (trie char (trie-children (first lst)) #t next-prefix) (rest lst))] |
34 | | - [else ;; move to the right |
35 | | - (cons (first lst) |
36 | | - (create-children char-list (rest lst) prefix-chars))])) |
| 28 | + (cond |
| 29 | + ;; children are empty, return list of empty children |
| 30 | + [(empty? lst) (list (trie char empty #t next-prefix))] |
| 31 | + ;; less than, put it to the left |
| 32 | + [(< char (trie-char (first lst))) (cons (trie char empty #t next-prefix) lst)] |
| 33 | + [(equal? char (trie-char (first lst))) ;; equal, step down a level |
| 34 | + (cons (trie char (trie-children (first lst)) #t next-prefix) (rest lst))] |
| 35 | + ;; move to the right |
| 36 | + [else (cons (first lst) (create-children char-list (rest lst) prefix-chars))])) |
37 | 37 |
|
38 | 38 | ;; contract: (listof char?) (listof trie?) integer? -> (listof trie?) |
39 | 39 | (define (handle-intern-letter char-list lst prefix-chars) |
40 | 40 | (define char (first char-list)) |
41 | | - ; (define next-prefix (append prefix-chars (list char))) |
| 41 | + ; (define next-prefix (append prefix-chars (list char))) |
42 | 42 | (define next-prefix (push-back prefix-chars char)) |
43 | | - (cond [(empty? lst) ;; no children, pop off front and step down |
44 | | - (list (trie char (create-children |
45 | | - (rest char-list) empty next-prefix) #f next-prefix))] |
46 | | - [(< char (trie-char (first lst))) ;; place where it is, pop off front and go |
47 | | - (cons (trie char (create-children |
48 | | - (rest char-list) empty next-prefix) #f next-prefix) lst)] |
49 | | - [(= char (trie-char (first lst))) ;; equal, step down |
50 | | - (cons (trie char (create-children (rest char-list) (trie-children (first lst)) next-prefix) |
51 | | - (trie-end-word? (first lst)) |
52 | | - (trie-word-up-to (first lst))) |
53 | | - (rest lst))] |
54 | | - [else ; move to the right |
55 | | - (cons (first lst) |
56 | | - (create-children char-list (rest lst) prefix-chars))])) |
| 43 | + (cond |
| 44 | + [(empty? lst) ;; no children, pop off front and step down |
| 45 | + (list (trie char (create-children (rest char-list) empty next-prefix) #f next-prefix))] |
| 46 | + [(< char (trie-char (first lst))) ;; place where it is, pop off front and go |
| 47 | + (cons (trie char (create-children (rest char-list) empty next-prefix) #f next-prefix) lst)] |
| 48 | + [(equal? char (trie-char (first lst))) ;; equal, step down |
| 49 | + (cons (trie char |
| 50 | + (create-children (rest char-list) (trie-children (first lst)) next-prefix) |
| 51 | + (trie-end-word? (first lst)) |
| 52 | + (trie-word-up-to (first lst))) |
| 53 | + (rest lst))] |
| 54 | + ; move to the right |
| 55 | + [else (cons (first lst) (create-children char-list (rest lst) prefix-chars))])) |
57 | 56 |
|
58 | 57 | ;; contract: trie? string? integer? -> trie? |
59 | 58 | (define (insert root-trie word) |
60 | 59 | (define char-list (string->list word)) |
61 | | - (trie |
62 | | - (trie-char root-trie) |
63 | | - (create-children char-list (trie-children root-trie) empty) |
64 | | - (trie-end-word? root-trie) |
65 | | - (trie-word-up-to root-trie))) |
| 60 | + (trie (trie-char root-trie) |
| 61 | + (create-children char-list (trie-children root-trie) empty) |
| 62 | + (trie-end-word? root-trie) |
| 63 | + (trie-word-up-to root-trie))) |
66 | 64 |
|
67 | 65 | ;; contract: trie? trie? -> boolean? |
68 | 66 | (define (trie<? trie-node1 trie-node2) |
69 | 67 | (< (trie-char trie-node1) (trie-char trie-node2))) |
70 | 68 |
|
71 | | - |
72 | 69 | ;; contract: trie? (listof string?) -> trie? |
73 | 70 | (define (build-trie-from-list-of-words trie list-of-words) |
74 | 71 | (cond |
75 | | - [(= (length list-of-words) 1) |
76 | | - (insert trie (first list-of-words))] |
77 | | - [else |
78 | | - (build-trie-from-list-of-words |
79 | | - (insert trie (first list-of-words)) |
80 | | - (rest list-of-words))])) |
| 72 | + [(= (length list-of-words) 1) (insert trie (first list-of-words))] |
| 73 | + [else (build-trie-from-list-of-words (insert trie (first list-of-words)) (rest list-of-words))])) |
81 | 74 |
|
82 | 75 | ;; ------------------ SORTING ---------------------- ;; |
83 | 76 |
|
84 | 77 | (define (trie-sort list-of-words) |
85 | 78 | (define new-trie (build-trie-from-list-of-words empty-trie list-of-words)) |
86 | 79 | (pre-order new-trie)) |
87 | 80 |
|
88 | | - ;; THIS ONE WORKS (using con and flatten) |
| 81 | +;; THIS ONE WORKS (using con and flatten) |
89 | 82 | ;; contract: trie? -> (listof string?) |
90 | 83 | (define (pre-order trie-node) |
91 | 84 | (if (trie-end-word? trie-node) |
|
0 commit comments