-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_03.hs
More file actions
90 lines (60 loc) · 2.05 KB
/
Copy pathChapter_03.hs
File metadata and controls
90 lines (60 loc) · 2.05 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
module Chapter_03 where
-- Exercise 3.1
-- What are the types of the following values?
e3_1_1 = ['a','b','c']
e3_1_1 :: [Char]
e3_1_2 = ('a','b','c')
e3_1_2 :: (Char, Char, Char)
e3_1_3 = [(False,'O'),(True,'1')]
e3_1_3 :: [(Bool, Char)]
e3_1_4 = ([False,True],['0','1'])
e3_1_4 :: ([Bool], [Char])
e3_1_5 = [tail, init, reverse]
e3_1_5 :: [[a] -> [a]]
-- Exercise 3.2
-- Write down definitions that have the following types; it does not matter what the definitions actually do as long as they are type correct. The type of your defined function may be more general than the types defined below.
bools :: [Bool]
bools = [True, False]
nums :: [[ Int ]]
nums = [[1,2], [3,4]]
add :: Num a => a -> a -> a -> a
add a b c = a + b + c
copy :: b -> (b, b)
copy b = (b, b)
apply :: (t1 -> t2) -> t1 -> t2
apply f x = f x
-- Exercise 3.3 (**)
-- What are the types of the following functions?
-- Hint: take care to include the necessary class constraints in the types if the functions are defined using overloaded operators.
second xs = head (tail xs)
second :: [a] -> a
swap (x,y) = (y,x)
swap :: (a, b) -> (b, a)
pair x y = (x,y)
pair :: x -> y -> (x,y)
double x = x*2
double :: Num x => x -> x
palindrome xs = reverse xs == xs
palindrome :: [Char] -> Bool
twice f x = f (f x)
twice :: (x -> x) -> x -> x
-- Exercise 3.4 (*)
-- Check your answers to the preceding three questions using GHCi.
-- Copy and paste your ghci sesssion into the block comment below.
{-
Prelude> double x = x*2
Prelude> :type double
double :: Num a => a -> a
Prelude> palindrome xs = reverse xs == xs
Prelude> :type palindrome
palindrome :: Eq a => [a] -> Bool
Prelude> twice f x = f (f x)
Prelude> :type twice
twice :: (t -> t) -> t -> t
-}
-- Exercise 3.5 (**)
-- Why is it not feasible in general for function types to be instances of the Eq class? When is it feasible? Hint: two functions of the same type are equal if they always return equal results for equal arguments.
-- Type your answer into the block comment below.
{-
you cannot test EVERY input
-}