-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
54 lines (42 loc) · 1.38 KB
/
Main.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
-- option 1 (https://github.com/PiotrJustyna/haskell-anywhere):
-- ./ghci.bat C:\Users\piotr_justyna\Documents\github\programming-in-haskell\part1_chapter6_exercise9
-- option 2 (stack):
-- stack ghci
-- option 3 (ghci):
-- ghci
--
-- :load Main
main = do
putStrLn "sum' ([] :: [Int])"
putStrLn . show $ sum' ([] :: [Int])
putStrLn "sum' [1, 2, 3]"
putStrLn . show $ sum' [1, 2, 3]
putStrLn "take' 34 ([] :: [Int])"
putStrLn . show $ take' 34 ([] :: [Int])
putStrLn "take' (-1) [1, 2, 3]"
putStrLn . show $ take' (-1) [1, 2, 3]
putStrLn "take' 0 [1, 2, 3]"
putStrLn . show $ take' 0 [1, 2, 3]
putStrLn "take' 2 [1, 2, 3]"
putStrLn . show $ take' 2 [1, 2, 3]
putStrLn "take' 4 [1, 2, 3]"
putStrLn . show $ take' 4 [1, 2, 3]
-- This causes an error (purposefully), so I commented it out.
-- putStrLn "last' ([] :: [Int])"
-- putStrLn . show $ last' ([] :: [Int])
putStrLn "last' [1]"
putStrLn . show $ last' [1]
putStrLn "last' [1, 2, 3]"
putStrLn . show $ last' [1, 2, 3]
sum' :: Num a => [a] -> a
sum' [] = 0
sum' (x:xs) = x + (sum' xs)
take' :: Show a => Int -> [a] -> [a]
take' _ [] = []
take' x (y:ys)
| x <= 0 = []
| otherwise = y : (take' (x - 1) ys)
last' :: Show a => [a] -> a
last' [] = error "empty list"
last' (x:[]) = x
last' (x:xs) = last' xs