-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.hs
More file actions
152 lines (97 loc) · 1.9 KB
/
Copy pathgcd.hs
File metadata and controls
152 lines (97 loc) · 1.9 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
mod 5 2
mod 351 38
mod 3 1
gcd 50 25
mod 49 14
mod 14 7
:{
let gcd 0 y = y
gcd x y
| x < 0 = gcd (-x) y
| y < 0 = gcd x (-y)
| x > y = gcd mxy y
| otherwise = gcd y x
where mxy = mod x y
:}
gcd 49 21
:{
let coprime x y
| gcd x y == 1 = True
| otherwise = False
:}
coprime 41 51
coprime 40 50
-- Eulers' Totient Phi
let phi x = map (coprime x) [1..(x-1)]
phi 5
:{
let countTrue [] n = n
countTrue (x:xs) n
| x == True = countTrue xs n+1
| otherwise = countTrue xs n
:}
-- could just foldr with a lambda istrue?
phi 8
countTrue (phi 8) 0
countTrue (phi 16) 0
countTrue (phi 10) 0
let tot n = countTrue (phi n) $ 0
tot 10
let primeF x y = map (gcd x) [1..y]
primeF 150 150
150 / 6
True (And) False
True && False
not True
not False
:t (&&)
-- doesn't work
:{
let and :: Bool -> Bool -> Bool
and x y
| True True = True
| True False = False
| False True = False
| False False = False
:}
:{
let and True True = True
and _ _ = False
:}
:{
let and True True = True
and _ _ = False
:}
:{
let or False False = False
or _ _ = True
:}
and True False
or True (3 == 4)
:{
let not' :: Bool -> Bool
not' True = False
not' False = True
:}
not' (3 == 4)
-- how to do this point free?
let nand x y = not $ and x y
let nor x y = not $ or x y
let xand x y = or (and x y) (nor x y)
let xor x y = not $ xand x y
xor True False
xor False True
xor True True
xor False False
let implies a b = or (not a) b
:{
let table :: (Bool -> Bool -> Bool) -> IO ()
table f = mapM_ putStrLn [show a ++ " " ++ show b ++ " " ++ show (f a b)
| a <- [True, False], b <- [True, False]]
:}
:t or'
table (\a b -> (and a (or a b)))
map (\n -> replicate n 'a') [1..5]
map (\n -> replicate n "ab") [1..5]
fmap (\n -> replicate n 'a') (1 <| 3 <| 5 <| empty)
1 0 0 1