-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSA2.hs
100 lines (72 loc) · 2.16 KB
/
FSA2.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module FSA2
where
import Data.List
fibon n = fibon' n 0 1 0
fibon' n x y k = if k == n then x
else fibon' n y (x+y) (k+1)
fix :: (a -> a) -> a
fix f = f (fix f)
fib :: Integer -> Integer
fib n = fib2 0 1 n
fib2 :: Integer -> Integer -> Integer -> Integer
fib2 = fix (\ f x y n ->
if n == 0 then x
else f y (x+y) (n-1))
run :: Integer -> [Integer]
run n = run1 [n]
run1 :: [Integer] -> [Integer]
run1 = fix (\ f ns ->
let
n = head ns
in
if n == 1 then ns
else if even n then f (div n 2:ns)
else f (3*n+1:ns))
while :: (a -> Bool) -> (a -> a) -> a -> a
while p f = \ x ->
if p x then while p f (f x)
else x
g = while even (`div` 2)
lfp :: Eq a => (a -> a) -> a -> a
lfp f = until (\x -> x == f x) f
lfp' :: Eq a => (a -> a) -> a -> a
lfp' f = while (\x -> x /= f x) f
lf :: Eq a => (a -> a) -> a -> a
lf f = fix
(\ g x -> if x == f x then x else g (f x))
whiler :: (a -> Bool) -> (a -> a) -> (a -> b) -> a -> b
whiler p f r = r . while p f
infixl 1 $$
($$) :: a -> (a -> b) -> b
($$) = flip ($)
fb :: Int -> Int
fb n = (0,1,0) $$
whiler (\ (_,_,k) -> k<n)
(\ (x,y,k) -> (y,x+y,k+1))
(\ (x,_,_) -> x)
eucl m n = (m,n) $$
whiler
(\ (x,y) -> x /= y)
(\ (x,y) -> if x > y then (x-y,x)
else (x,y-x))
fst
repeat :: (a -> a) -> (a -> Bool) -> a -> a
repeat f p = while (not.p) f . f
repeatr :: (a -> a) -> (a -> Bool) -> (a -> b) -> a -> b
repeatr f p r = whiler (not.p) f r . f
for :: [a] -> (a -> b -> b) -> b -> b
for [] f y = y
for (x:xs) f y = for xs f (f x y)
fact :: Integer -> Integer
fact n = 1 $$ for [1..n] (*)
factorial :: Integer -> Integer
factorial m = (1,m) $$
whiler (\ (_,n) -> n /= 0)
(\ (t,n) -> (n*t,n-1))
fst
forr :: [a] -> (a -> b -> b) -> (b -> c) -> b -> c
forr xs f r = r . for xs f
fordown :: [a] -> (a -> b -> b) -> b -> b
fordown = for . reverse
forrdown :: [a] -> (a -> b -> b) -> (b -> c) -> b -> c
forrdown = forr . reverse