-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex5.hs
More file actions
48 lines (36 loc) · 1.04 KB
/
ex5.hs
File metadata and controls
48 lines (36 loc) · 1.04 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
import Prelude hiding (or, minimum, lookup)
triangle :: Int -> Int
triangle n = sum [0..n]
or :: [Bool] -> Bool
or [] = False
or (x:xs) = x || or xs
minimum :: [Int] -> Int
minimum [x] = x
minimum (x:xs) = min x (minimum xs)
count :: Int -> [Int] -> Int
count x [] = 0
count x (y:ys) = if x==y then 1 + count x ys else count x ys
lookup :: (Eq a) => a -> [(a,b)] -> b
lookup key [] = error "key not in dict"
lookup key (pair:dict) = if key== fst pair then snd pair else lookup key dict
euclid :: Int -> Int -> Int
euclid x y
| x == y = x
| x > y = euclid (x-y) y
|otherwise = euclid x (y-x)
test1 = euclid 6 27
-- > 3
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
altMap f g [] = []
altMap f g [x] = [f x]
altMap f g (x:y:zs) = [f x ] ++ [g y] ++ altMap f g zs
test2 = altMap (+10) (+100) [1,2,3,4,5]
-- > [11,102,13,104,15]
luhnDouble :: Int -> Int
luhnDouble x = if x*2 > 9 then x*2 - 9 else x*2
luhn :: [Int] -> Bool
luhn xs = sum (altMap id luhnDouble (reverse xs)) `mod` 10 == 0
test3 = luhn [1,7,8,4]
-- > True
test4 = luhn [4,7,8,3]
-- > False