This repository was archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhone.hs
82 lines (64 loc) · 2.24 KB
/
Phone.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
module Phone where
import qualified Data.Char as Char
import qualified Data.List as List
import qualified Data.Maybe as Maybe
type Digit = Char
type Presses = Int
newtype Key = Key ( [Digit] )
newtype Phone = Phone [ Key ]
myPhone :: Phone
myPhone = Phone [ Key "1" , Key "2abc", Key "3def"
, Key "4ghi" , Key "5jkl", Key "6mno"
, Key "7pqrs", Key "8tuv", Key "9wxyz"
, Key "*" , Key "0+ _", Key "#.,"
]
charToAction :: Char -> Phone -> [ Maybe (Digit, Presses) ]
charToAction k phone@(Phone p)
| Char.isAsciiUpper k = Just ('*', 1) : charToAction (Char.toLower k) phone
| otherwise = [ foldl f Nothing p ]
where f Nothing (Key a) = x
where idx = List.elemIndex k a
x = if Maybe.isNothing idx
then Nothing
else Just (head a, Maybe.fromMaybe 0 idx)
f b (Key a) = b
textToAction :: String -> Phone -> [ Maybe (Digit, Presses) ]
textToAction text phone = foldl f [] text
where f b a = b ++ charToAction a phone
countActions :: [ Maybe (Digit, Presses) ] -> Presses
countActions = foldl f 0
where f b (Just ( _, p)) = b + p
f b Nothing = b
-- >>> charToAction '~' myPhone
-- >>> action = textToAction "Hello!, how are you" myPhone
-- >>> action
-- >>> countActions action
-- [Nothing]
-- [Just ('*',1),Just ('4',2),Just ('3',2),Just ('5',3),Just ('5',3),Just ('6',3),Nothing,Just ('#',2),Just ('0',2),Just ('4',2),Just ('6',3),Just ('9',1),Just ('0',2),Just ('2',1),Just ('7',3),Just ('3',2),Just ('0',2),Just ('9',3),Just ('6',3),Just ('8',2)]
-- 42
convo :: [ String ]
convo =
[ "Wanna play 20 questions",
"Ya",
"U 1st haha",
"Lol ok. Have u ever tasted alcohol",
"Lol ya",
"Wow ur cool haha. Ur turn",
"Ok. Do u think I am pretty Lol",
"Lol ya",
"Just making sure rofl ur turn" ]
data Expr = Lit Integer
| Add Expr Expr
eval :: Expr -> Integer
eval (Lit i) = i
eval (Add a b) = eval a + eval b
printExpr :: Expr -> String
printExpr (Lit i) = show i
printExpr (Add a b) = printExpr a ++ " + " ++ printExpr b
-- >>> a1 = Add (Lit 9001) (Lit 1)
-- >>> a2 = Add a1 (Lit 20001)
-- >>> a3 = Add (Lit 1) a2
-- >>> eval a3
-- >>> printExpr a3
-- 29004
-- "1 + 9001 + 1 + 20001"