-
Notifications
You must be signed in to change notification settings - Fork 0
/
Definition.hs
82 lines (61 loc) · 3.56 KB
/
Definition.hs
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
module Definition
( Definition(..),
Solutions,
Type(..),
correct,
readDef,
solution,
addTranslation,
showSolutions
) where
import qualified Data.Text as T
data Type = Noun | Verb | Adjective | Pronoun | Adverb | Conjunction | Term deriving (Eq)
data Solutions = Solutions {
nouns :: [String],
verbs :: [String],
adjectives :: [String],
pronouns :: [String],
adverbs :: [String],
conjunctions :: [String],
terms :: [String]
} deriving (Show, Read)
data Definition = Definition {
word :: String,
solutions :: Solutions
} deriving (Show, Read)
-- correct ////////////////////////////////////////////////////////////////
correct :: String -> Solutions -> Bool
correct answer solutions = correct' answer $ concatSolutions solutions
-- correct' ////////////////////////////////////////////////////////////////
correct' :: String -> [String] -> Bool
correct' _ [] = False
correct' answer (x:xs)
| answer==x = True
| otherwise = correct' answer xs
-- concatSolutions ////////////////////////////////////////////////////////////////
concatSolutions :: Solutions -> [String]
concatSolutions Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t} = n ++ v ++ adj ++ p ++ adv ++ c ++t
-- read ////////////////////////////////////////////////////////////////
readDef :: String -> Definition
readDef input = read input
-- solution ////////////////////////////////////////////////////////////////
solution :: Solutions
solution = Solutions {nouns = [], verbs = [], adjectives = [], pronouns = [], adverbs = [], conjunctions = [], terms = []}
-- addTranslation ////////////////////////////////////////////////////////////////
addTranslation :: Type -> Solutions -> String -> Solutions
addTranslation ty (Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t}) trans
| ty==Noun = Solutions {nouns = trans : n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t}
| ty==Verb = Solutions {nouns = n, verbs = trans : v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t}
| ty==Adjective = Solutions {nouns = n, verbs = v, adjectives = trans : adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t}
| ty==Pronoun = Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = trans : p, adverbs = adv, conjunctions = c, terms = t}
| ty==Adverb = Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = trans : adv, conjunctions = c, terms = t}
| ty==Conjunction = Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = trans : c, terms = t}
| ty==Term = Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = trans : t}
| otherwise = Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t}
-- showSolutions ////////////////////////////////////////////////////////////////
showSolutions :: Solutions -> String
showSolutions Solutions {nouns = n, verbs = v, adjectives = adj, pronouns = p, adverbs = adv, conjunctions = c, terms = t} = (showType "Nouns" n) ++ (showType "Verbs" v) ++ (showType "Adjectives" adj) ++ (showType "Pronouns" p) ++ (showType "Adverbs" adv) ++ (showType "Conjunctions" c) ++ (showType "Terms" t)
-- showType ////////////////////////////////////////////////////////////////
showType :: String -> [String] -> String
showType _ [] = ""
showType ty sols = foldl (\deck sol -> deck ++ "\n\t-" ++ sol) ("\n * " ++ ty) sols