-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion.hs
48 lines (36 loc) · 1.21 KB
/
recursion.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
-- recursion.hs
incTimes :: (Eq a, Num a) => a -> a -> a
incTimes 0 n = n
incTimes times n = 1 + (incTimes (times - 1) n)
applyTimes :: (Eq a, Num a) => a -> (b -> b) -> b -> b
applyTimes 0 f b = b
applyTimes n f b = f (applyTimes (n - 1) f b)
incTimes' :: (Eq a, Num a) => a -> a -> a
incTimes' times n = applyTimes times (+1) n
applyTimes' :: (Eq a, Num a) => a -> (b -> b) -> b -> b
applyTimes' 0 f b = b
applyTimes' n f b = f . applyTimes' (n-1) f $ b
type Numerator = Integer
type Denomenator = Integer
type Quotient = Integer
dividedBy :: Numerator -> Denomenator -> Quotient
dividedBy = div
dividedBy' :: (Integral a) => a -> a -> (a, a)
dividedBy' num denom = go num denom 0
where go n d count
| n < d = (count, n)
| otherwise = go (n - d) d (count + 1)
data DividedResult =
Result Integer
| DividedByZero deriving Show
fixedDividedBy :: Integer -> Integer -> DividedResult
fixedDividedBy n d
| d == 0 = DividedByZero
| otherwise = go (abs n) (abs d) 0 ((signum n) * (signum d))
where go n d count s
| n < d = Result (s * count)
| otherwise = go (n - d) d (count + 1) s
mc91 :: Integer -> Integer
mc91 n
| n > 100 = n - 10
| otherwise = mc91 $ mc91 (n + 11)