-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise12.hs
More file actions
129 lines (100 loc) · 3.88 KB
/
Copy pathExercise12.hs
File metadata and controls
129 lines (100 loc) · 3.88 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{-
** NOT OVERLOADED **
elemInt :: Int -> [Int] -> Bool
Problem: Each time we want to check membership of a list of a different type we will have to define yet another - very similar - function.
elemBool :: Bool -> [Bool] -> Bool
Way out of this problem:
** OVERLOADED **
elem :: Eq a => a -> [a] -> Bool
-}
-- 12.1
-- How would you define /= from equality? What is the type of /=?
{-
(/=) :: Eq a => a -> a -> Bool
(/=) a b = not $ (==) a b
-}
-- 12.2
-- Define function numEqual which takes a list of items and an item, and returns the number of times x occurs in xs
-- I think it will be - numEqual :: Eq a => [a] -> a -> Int
-- actual type
numEqual :: (Num n, Eq a) => [a] -> a -> n
numEqual [] _ = 0
numEqual (x:xs) a
| x == a = 1 + numEqual xs a
| otherwise = 0 + numEqual xs a
-- probably I missunderstanding something about Num type
-- 12.3
-- define functions
-- If a list doesn't contain a pair that we are looking for, then we don't know which element of a list function should produce, therefore with our type declaration we have to produce element b, and the only way to do this is to make an exception. (As I think so)
oneLookupFirst :: Eq a => [(a,b)] -> a -> b
oneLookupFirst [] a = error "No such element in the list."
oneLookupFirst (x:xs) a
| a == fst x = snd x
| otherwise = oneLookupFirst xs a
oneLookupSecond :: Eq b => [(a,b)] -> b -> a
oneLookupSecond [] a = error "No such element in the list."
oneLookupSecond (x:xs) b
| b == snd x = fst x
| otherwise = oneLookupSecond xs b
-- another way to define those functions
oneLookUpFirstFoldr list a = foldr (\(x,y) acc -> if x == a then y else acc) (error "No such element in the list") list
---------------------------------------------------------------------------------------
class Visible a where
toString :: a -> String
size :: a -> Int
instance Visible Char where
toString ch = [ch]
size _ = 1
instance Visible Bool where
toString True = "True"
toString False = "False"
size _ = 1
instance Visible a => Visible [a] where
toString = concat . map toString
size = foldr (+) 1 . map size
-- 12.4
-- How would you make Bool, pair types (a, b), and triple types, (a, b, c), into Visible types?
instance (Visible a, Visible b) => Visible (a, b) where
toString (a, b) = (++) (toString a) (toString b)
size _ = 1
instance (Visible a, Visible b, Visible c) => Visible (a, b, c) where
toString (a, b, c) = (toString a) ++ (toString b) ++ (toString c)
size _ = 1
-- 12.5
-- Write a function to convert an integer into a String, and hence show how Int can be an instance of Visible
instance Visible Int where
toString = show
size _ = 1
-- 12.6
-- What is the type of the function
-- compare x y = size x <= size y
-- compare :: (Visible a, Visible b) => a -> b -> Bool
-- 12.7
-- Complete the default definitions for the class Ord
{-
x >= y = x > y || x == y
x < y = y > x
min x y = \x y -> if x > y && y < x then x else y
max x y = \x y -> if x < y && y > x then x else y
compare x y = \x y -> | x > y = GT | x < y = LT | otherwise = EQ
-}
-- 12.8
-- complete the following instance declarations
{-
instance (Ord a, Ord b) => Ord (a,b) where
instance Ord b => Ord [b] where
-}
-- 12.9
-- Investigate the Haskell definition of '<' on the type Bool and (t1,t2,...,tk)
-- 12.10
-- Define a function TODO investigate what the hell is this
--showBoolFun :: (Bool -> Bool) -> String
-- once again shitty exercises
-- Generalize this to
-- showBoolFunGen :: (a -> String) -> (Bool -> a) -> String
{-
** Short summary **
Overloading - functions and operators can have different definitions as different types
-- mechanism which enables this is the system of classes
A class definition contains a signature which contains the names and types of operations which must be supplied if a type is to be a member of the class. For a particular type. the function definitions are contained in an instance declaration.
-}