-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter4.hs
82 lines (69 loc) · 1.57 KB
/
Chapter4.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
77
78
79
80
81
82
-- Defining Functions
-- Exercise one
halve :: [a] -> ([a], [a])
halve a = (take ((length a) `div` 2) a,drop ((length a) `div` 2) a)
-- Exercise two
thirda :: [a] -> a
thirda a = head $ tail $ tail a
thirdb :: [a] -> a
thirdb a = a !! 2
thirdc :: [a] -> a
thirdc (_:(_:(a:_))) = a
--Exercise 3
safetaila :: [a] -> [a]
safetaila a = if length a == 0
then []
else tail a
safetailb :: [a] -> [a]
safetailb a | length a == 0 = []
| otherwise = tail a
safetailc :: [a] -> [a]
safetailc [] = []
safetailc a = tail a
-- Exercise 4
-- Commented out because ambiguous
{-
(?) :: Bool -> Bool -> Bool
True ? True = True
True ? False = True
False ? False = True
False ? True = True
-}
{-
(?) :: Bool -> Bool -> Bool
True ? _ = True
_ ? True = True
False ? _ = False
-}
{-
(?) :: Bool -> Bool -> Bool
False ? False = False
_ ? _ = True
-}
{-}
(?) :: Bool -> Bool -> Bool
False ? b = b
True ? _ = True
-}
-- Exercise 5
and' :: Bool -> Bool -> Bool
and' a b = if a == True
then if b == True
then True
else False
else False
-- Exercise 6
and'' :: Bool -> Bool -> Bool
and'' a b = if a == True
then b
else False
-- Exercise 7
mult :: Int -> (Int -> (Int -> Int))
mult = \x -> (\y -> (\z -> x * y * z))
-- Exercise 8
luhnDouble :: Int -> Int
luhnDouble x = if x * 2 > 9
then x * 2 - 9
else x * 2
luhn :: Int -> Int -> Int -> Int -> Bool
luhn a b c d = (mod (sum (map luhnDouble [a,c]) + b + d) 10) == 0