-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter-4.hs
95 lines (67 loc) · 1.51 KB
/
chapter-4.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
83
84
85
86
87
88
89
90
91
92
93
94
95
-- Chapter 4 exercises
-- Exercises - Mood Swing
-- 1 - Mood.
-- 2 - reverse or `Woot`.
-- 3 - Type signature can't have values. It should have types.
-- 4 -
data Mood = Blah | Woot deriving Show
changeMood :: Mood -> Mood
changeMood Blah = Woot
changeMood Woot = Blah
-- 5 - Done.
-- Exercises - Find the Mistakes
-- 1 -
not True && True
-- 2 -
not (x == 6)
-- 3 -
(1 * 2) > 5
-- 4 -
"Merry" > "Happy"
-- 6 -
['1', '2', '3'] ++ "look at me!"
-- Chapter exercises
awesome = ["Papuchon", "curry", ":)"]
also = ["Quake", "The Simons"]
allAwesome = [awesome, also]
-- 1 - `length` must be [a] -> Int
-- 2
-- a - 5
-- b - 3
-- c - 2
-- d - 5
-- 3 -
-- `6 / length [1, 2, 3]` must be resulting in an error since `legnth` returns
-- `Int` and we want a `Fractional` cosntraint.
-- 4 - We can use the `div` function.
-- 5 - Type will be `Bool` and the result will be `True`.
-- 6 - Type will be `Bool` and the result will be `False`.
-- 7
-- a - Will work - True
-- b - Wont' work - List must have elements with the same type.
-- c - Will work - 5
-- d - Will work - False
-- e - Won't work - && needs two Bools.
-- 8
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome x = reverse x == x
-- 9
myAbs :: Integer -> Integer
myAbs x = if x >= 0 then x else (-x)
-- 10
f :: (a, b) -> (c, d) -> ((b, d), (a, c))
f x y = ((snd x, snd y), (fst x, fst y))
-- Correcting syntax
-- 1
x = (+)
f' xs = x w 1
where w = length xs
-- 2
\x -> x
-- 3
f'' (a, b) = a
-- Match the function names to their types
-- 1 - c
-- 2 - b
-- 3 - a
-- 4 - d