-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_with_DL.hs
58 lines (41 loc) · 1.51 KB
/
logging_with_DL.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
type DList a = [a] -> [a]
empty :: DList a
empty = \xs -> xs -- returns a lambda that returns the lists itself
sngl :: a -> DList a
sngl x = \xs -> x : xs -- returns a lambda that appends x to the supplied list xs to the lambda
app :: DList a -> DList a -> DList a
ys `app` zs = \xs -> ys (zs xs) -- returns a lambda that gets a list and supply it to lambda that gets a list which then supply it to another lambda that gets a list
fromList :: [a] -> DList a
fromList ys = \xs -> ys ++ xs -- returns a lambda that gets a list and append it to the list that was previously supplied
toList :: DList a -> [a]
toList ys = ys [] -- applies the series of lambdas to empty list
type DLog = DList (Integer,Integer)
gcdLog :: Integer -> Integer -> DLog -> (Integer, DLog)
gcdLog a b dlog
| a == b = (a,log')
| a > b = gcdLog (a-b) b log'
| otherwise = gcdLog a (b-a) log'
where
log' = dlog `app` sngl (a,b)
gcdGetLog :: Integer -> Integer -> [(Integer,Integer)]
gcdGetLog a b = toList (snd (gcdLog a b empty))
-- BETTER
emptyB :: DList a
emptyB = id
snglB :: a -> DList a
snglB = (:)
appB :: DList a -> DList a -> DList a
appB = (.)
fromListB :: [a] -> DList a
fromListB = (++)
toListB :: DList a -> [a]
toListB = ($[])
gcdLogB :: Integer -> Integer -> DLog -> (Integer, DLog)
gcdLogB a b dlog
| a == b = (a,log')
| a > b = gcdLog (a-b) b log'
| otherwise = gcdLog a (b-a) log'
where
log' = dlog `appB` snglB (a,b)
gcdGetLogB :: Integer -> Integer -> [(Integer,Integer)]
gcdGetLogB a b = toList (snd (gcdLog a b emptyB))