-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-Lab.hs
76 lines (51 loc) · 1.65 KB
/
2-Lab.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
------------- Exercise One ----------------------
-- Task: return last element without using last cmd (2 examples)
last' :: [a] -> a
last' xs = xs !! (length xs - 1)
last'' :: [a] -> a
last'' xs = head (reverse xs)
------------- Exercise Two ----------------------
-- Task: return the third element with different techniques
third :: [a] -> a
third xs = head (tail (tail xs))
third' :: [a] -> a
third' xs = xs !! 2
third'' :: [a] -> a
third'' (x:y:z:xs) = z
------------ Exercise Three ---------------------
-- Task: Smart tail, returns empty list if there is no tail (3 different techniquies)
safetail :: [a] -> [a]
safetail xs = if null' xs == True then [] else tail xs
safetail' :: [a] -> [a]
safetail' xs
| null' xs == True = []
| otherwise = tail xs
safetail'' :: [a] -> [a]
safetail'' [] = []
safetail'' (x:xs) = xs
null' :: [a] -> Bool
null' xs
| length xs == 0 = True
| otherwise = False
------------- Exercise Four ---------------------
-- Task: split even length list in two parts
halve :: [a] -> ([a],[a])
halve xs = (x,y)
where
x = take mid xs
y = drop mid xs
mid = length xs `div` 2
------------- Exercise Five ---------------------
--enc :: Int -> String -> String
------------- Exercise Six ----------------------
-- Task: create an algorithm that check if 4-digit number is lugh number
luhnDouble :: Int -> Int
luhnDouble x
| x * 2 > 9 = x * 2 - 9
| otherwise = x * 2
luhn :: Int -> Int -> Int -> Int ->Bool
luhn a b c d
| total `mod` 10 == 0 = True
| otherwise = False
where
total = luhnDouble a + b + luhnDouble c + d