-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12recursionPatternMatching.hs
More file actions
51 lines (31 loc) · 1.05 KB
/
12recursionPatternMatching.hs
File metadata and controls
51 lines (31 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
-- Implement 'take
-- Example:
-- take 3 [1,2,3,4]
-- [1,2,3]
myTake num (elem:lis) = if num > (length (lis) + 1) then (elem:lis) -- edge case:
else if num == 0 then []
else elem : (myTake (num - 1) lis)
-------------------------------------
-- Implement GCD of two numbers
-- GCD == largest number that divides both the numbers
-- to do
------------------------------------
-- switch case:
sayAmount n = case n of
1 -> "one"
2 -> "two"
n -> "lots"
-- same logic above using pattern matching:
sayAmount2 1 = "one"
sayAmount2 2 = "two"
sayAmount2 n = if even n then "lots" else "no"
-- more pattern matching
isEmpty [] = error "Empty list"
isEmpty _ = False
-- even more:
myHead (x:xs) = x
myHead [] = error "No head for empty list"
-- my tail
myTail (_:xs) = xs
-- The tail function in Haskell returns an error when called on an empty list. Modify myTail so that it does handle the case of an empty list by returning the empty list.
myTail [] = []