-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_01.hs
More file actions
52 lines (37 loc) · 1.05 KB
/
Copy pathChapter_01.hs
File metadata and controls
52 lines (37 loc) · 1.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
module Chapter_01 where
-- Exercise 1.1 (*)
-- Give another possible calculation for the result of double (double 2).
-- Complete the following block comment.
{-
double (double 2)
(double 2) + (double 2)
(2 + 2) + (double 2)
4 + (double 2)
4 + 4
8
-}
-- Exercise 1.2 (*)
-- Show that sum [x] = x for any number x.
-- Complete the following block comment.
{-
sum [1,2,3,4]
1 + (sum [2,3,4])
1 + (2 + (sum [3,4]))
1 + (2 + (3 + (sum [4])))
1 + (2 + (3 + (4 + (sum []))))
1 + (2 + (3 + (4 + 0))))
1 + (2 + (3 + 4)))
1 + (2 + 7)
1 + 9
10
-}
-- Exercise 1.3 (*)
-- Define a function myProduct that produces the product of a list of numbers, and show using your definition that myProduct [2,3,4] == 24.
-- Note: We use the name "myProduct" since the name product is already defined in the ghc Prelude.
--myProduct xs = if length xs == 1 then head xs else head xs * myProduct (tail xs)
--myProduct [2,3,4]
{-
Prelude> myProduct [] = 1; myProduct (n:ns) = n * myProduct ns;
Prelude> myProduct [1,2,3,4]
24
-}