-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week2.hs
65 lines (49 loc) · 1.44 KB
/
Week2.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
data Thing = Shoe
| Ship
| SealingWax
| Cabbage
| King
deriving Show
shoe :: Thing
shoe = Shoe
isSmall :: Thing -> Bool
isSmall Ship = False
isSmall King = False
isSmall _ = True
data FailableDouble = Failure
| OK Double
deriving Show
ex01 = Failure
ex02 = OK 3.4
safeDiv :: Double -> Double -> FailableDouble
safeDiv _ 0 = Failure
safeDiv x y = OK (x / y)
failureToZero :: FailableDouble -> Double
failureToZero Failure = 0
failureToZero (OK d) = d
data Person = Person String Int Thing
deriving Show
brent :: Person
brent = Person "Brent" 31 SealingWax
stan :: Person
stan = Person "Stan" 94 Cabbage
said :: Person
said = Person "Said" 31 SealingWax
getAge :: Person -> Int
getAge (Person _ a _) = a
baz :: Person -> String
baz p@(Person n _ _) = "The name field of (" ++ show p ++") is " ++ n
checkFav :: Person -> String
checkFav (Person n _ SealingWax) = n ++ ", you're my kind of preson!"
checkFav (Person n _ _) = n ++ ", your favourit thing is lame."
printAge :: Person -> String
printAge (Person _ a@age _) = "Your age is " ++ show a
caseFunction :: String -> Int
caseFunction input = case input of
[] -> 3
('H':s) -> length s
_ -> 7
failureToZero' :: FailableDouble -> Double
failureToZero' x = case x of
Failure -> 0
OK d -> d