-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreeletter.sml
52 lines (41 loc) · 1.24 KB
/
threeletter.sml
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
structure ThreeLetter =
struct
fun droplast l = List.take (l, List.length l - 1)
fun chop s = String.concat (map Char.toString (droplast (String.explode s)))
fun getWords wordlist =
let
val file = TextIO.openIn wordlist
fun addlines lines =
case TextIO.inputLine file of
SOME line => addlines (line::lines)
| NONE => rev lines
val words = map chop (addlines [])
in
droplast words
end
val scrabbleWords = getWords "scrabble3.txt"
val increpareWords = getWords "increpare3.txt"
(* only works if chars is a list of characters of length 3 *)
fun permute3 chars =
case chars of
[x, y, z] => [[x, y, z], [x, z, y], [y, x, z], [y, z, x], [z, x, y], [z, y, x]]
| _ => [chars]
fun anagrams s =
let
val chars = String.explode s
val permutes = permute3 chars
val anagrams = map (String.concat o (map Char.toString)) permutes
in
anagrams
end
fun member x l = List.exists (fn y => y = x) l
fun isWord dictionary w = member w dictionary
fun checkAnagramMembership dictionary word =
let
val anas = anagrams word
in
List.all (isWord dictionary) anas
end
fun findAnagrams dict =
List.filter (checkAnagramMembership dict) dict
end