-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter02.hs
77 lines (58 loc) · 2.24 KB
/
chapter02.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
-- ghc chapter02.hs
-- ./chapter02.exe
import System.IO
main = do
putStrLn "Chapter 02 - Start!"
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "head of [1..10]"
print (head [1..10])
putStrLn "tail of [1..10]"
print (tail [1..10])
putStrLn "!! to get n-th element (zero-indexed), so [1..10] !! 2 gives us.."
print ([1..10] !! 2)
putStrLn "take 3 [1..10]"
print (take 3 [1..10])
putStrLn "drop 3 [1..10]"
print (drop 3 [1..10])
putStrLn "length [1..10]"
print (length [1..10])
putStrLn "++ to concatonate lists, so [1..10] ++ [20..30] give us.."
print ([1..10] ++ [20..30])
putStrLn "revese [1..10]"
print (reverse [1..10])
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Factorial 5"
let factorial n = product [1..n]
print (factorial 5)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Mean average of [1..10]"
let mean_average xs = sum xs `div` length xs
print (mean_average [1..10])
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Few notes on indentation
let a = b + c
where
b = 1 -- b and c are local to a
c = 2
let d = a * 2
-- we can also use braces to further emphasise scope:
let a = b + c
where
{b = 1; -- b and c are local to a
c = 2};
let d = a * 2
-- ..or even write it on a single line:
let a = b + c where {b = 1; c = 2};
let d = a * 2;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Exercise 4"
putStrLn "Get last element of [1..10]"
print (last [1..10])
print (head (reverse [1..10]))
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Exercise 5"
putStrLn "init removes last element"
print (init [1..10])
print (reverse (tail (reverse [1..10])))
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Chapter 02 - Compete!"