-
Notifications
You must be signed in to change notification settings - Fork 0
/
259.rkt
115 lines (90 loc) · 3.87 KB
/
259.rkt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;; 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-reader.ss" "lang")((modname 259ex) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; Exercise 259
;; ------------
;; Use a local expression to organize the functions for rearranging words from
;; Word Games, the Heart of the Problem.
;; -----------------------------------------------------------------------------
(require 2htdp/batch-io)
(define LOCATION "/usr/share/dict/words")
; On LINUX: /usr/share/dict/words or /var/lib/dict/words
; On WINDOWS: borrow the word file from your Linux friend
; A Dictionary is a List-of-strings.
(define AS-LIST (read-lines LOCATION))
; ---
(define (arrangements w)
(local (; Word -> List-of-words
; creates all rearrangements of the letters in w
(define (arrangements-in w)
(cond
[(empty? w) (list '())]
[else (insert-everywhere/in-all-words (first w)
(arrangements-in (rest w)))]))
; 1String List-of-words -> List-of-words
(define (insert-everywhere/in-all-words s l)
(cond
[(empty? l) '()]
[else (append (insert-everywhere s (first l)) ; : Word -> List-of-words
(insert-everywhere/in-all-words s (rest l)) ; : List-of-words
)]))
; 1String Word -> List-of-words
; inserts s at all positions in w and makes a list of them
(define (insert-everywhere s w)
(insert-everywhere-aux s w (length w)))
; 1String Word Nat -> List-of-words
; inserts s at all positions in w at positions 0 to n
(define (insert-everywhere-aux s w n)
(cond
[(= 0 n) (list (insert-s-at-ith s w 0))]
[else (cons (insert-s-at-ith s w n)
(insert-everywhere-aux s w (sub1 n)))]))
; 1String Word Nat -> Word
; inserts s at ith position in w (i <= (length w))
(define (insert-s-at-ith s w i)
(cond
[(= i 0) (cons s w)]
[else (cons (first w) (insert-s-at-ith s (rest w) (sub1 i)))])))
(arrangements-in w)))
; ------- these functions are useful outside a local
; List-of-strings -> List-of-strings
; keeps all strings that are a part of the dictionary
(define (in-dictionary los d)
(cond
[(empty? los) '()]
[else (if (word-in-dic? (first los) d)
(cons (first los) (in-dictionary (rest los) d))
(in-dictionary (rest los) d))]))
; String -> Boolean
; is w a part of the dictionary?
(define (word-in-dic? w d)
(cond
[(empty? d) #false]
[else (or (string=? w (first d))
(word-in-dic? w (rest d)))]))
; List-of-words -> List-of-strings
; collapses each word is `words` to a string
(define (words->strings words)
(cond
[(empty? words) '()]
[else (cons (implode (first words))
(words->strings (rest words)))]))
; String -> Word
; converts s to the chosen word representation
(define (string->word s)
(explode s))
; -------
(define (alternative-words s)
(local ((define s-as-word (string->word s))
(define word-arrangements (arrangements s-as-word))
(define arrangements-as-strings (words->strings word-arrangements))
(define strings-in-dic (in-dictionary arrangements-as-strings AS-LIST)))
strings-in-dic))
; ----- tests -----
(check-expect (alternative-words "cat") (list "act" "cat"))
; List-of-strings -> Boolean
(define (all-words-from-rat? w)
(and
(member? "rat" w) (member? "art" w) (member? "tar" w)))
(check-satisfied (alternative-words "rat")
all-words-from-rat?)