-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter01.hs
49 lines (37 loc) · 1.61 KB
/
chapter01.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
-- ghc chapter01.hs
-- ./chapter01.exe
import System.IO
main = do
putStrLn "Chapter 01 - Start!"
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Total of 1 to 10"
let total = sum[1..10] -- List comprehension
print total
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Sorting"
let qsort :: [Int] -> [Int] -- qsort is array-of-int to array-of-int
qsort [] = [] -- base case
qsort (x:xs) = qsort smaller ++ [x] ++ qsort larger -- non-base case
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
let unsorted = [3, 10, 6, 2, 8, 5, 7, 4, 1, 9]
let sorted = qsort unsorted
putStrLn "Unsorted"
print unsorted
putStrLn "Sorted"
print sorted
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Exercise 1"
putStrLn "Doubling 4"
let double x = x * 2
print (double 4)
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Exercise 3"
let product :: [Int] -> Int
product [] = 1
product (x:xs) = x * product xs
putStrLn "product [1..5]"
print (product [1..5])
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
putStrLn "Chapter 01 - Compete!"