/
unihaskell.hs
291 lines (223 loc) · 8.93 KB
/
unihaskell.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env ghc
{-# LANGUAGE UnicodeSyntax #-}
{-|
Module : UniHaskell
Description : Convenience/golfing library
Copyright : (c) Sumant Bhaskaruni, 2018
License : MIT
Maintainer : bsumantb@gmail.com
Stability : stable
A convenience library for Haskell that also functions as a golfing library.
-}
module UniHaskell where
import Data.Bits
import Data.List hiding (find)
infixl 9 ‼
infixl 8 ≪, ≫
infixl 7 ×, ÷, %, //, ⋏
infixl 6 ⊻
infixl 5 ⋎
infixl 4 §
infixr 9 ∘
infixr 5 ⋅
infixr 3 ∧, ∨, ⊕, ⊙, ⊼, ⊽
infix 5 ∩, ∪, ∖, ⊗
infix 4 ≡, ≢, ≠, ≤, ≥, ∣, ∤, ∈, ∉, ∋, ∌, ⊆, ⊇, ⊈, ⊉, ⊂, ⊃, ⊄, ⊅
-- | @a ⊷ b@ returns the first argument, or @a@.
(⊷) ∷ a → b → a
a ⊷ b = a
-- | @a ⊶ b@ returns the second argument, or @b@.
(⊶) ∷ a → b → b
a ⊶ b = b
-- | @a ∧ b@ returns the logical AND of @a@ and @b@.
(∧) ∷ Bool → Bool → Bool
(∧) = (&&)
-- | @a ∨ b@ returns the logical OR of @a@ and @b@.
(∨) ∷ Bool → Bool → Bool
(∨) = (||)
-- | @a ⊕ b@ returns the logical XOR of @a@ and @b@.
(⊕) ∷ Bool → Bool → Bool
a ⊕ b = (a ∨ b) ∧ (a ⊼ b)
-- | @a ⊙ b@ returns the logical XNOR of @a@ and @b@.
(⊙) ∷ Bool → Bool → Bool
a ⊙ b = (a ∧ b) ∨ (a ⊽ b)
-- | @a ⊼ b@ returns the logical NAND of @a@ and @b@.
(⊼) ∷ Bool → Bool → Bool
(⊼) = (not ∘) ∘ (∧)
-- | @a ⊽ b@ returns the logical NOR of @a@ and @b@.
(⊽) ∷ Bool → Bool → Bool
(⊽) = (not ∘) ∘ (∨)
-- | @a ≡ b@ returns whether @a@ is equal to @b@.
(≡) ∷ Eq a ⇒ a → a → Bool
(≡) = (==)
-- | @a ≢ b@ returns whether @a@ is inequal to @b@.
(≢) ∷ Eq a ⇒ a → a → Bool
(≢) = (/=)
-- | @a ≠ b@ returns whether @a@ is inequal to @b@.
(≠) ∷ Eq a ⇒ a → a → Bool
(≠) = (/=)
-- | @a ≤ b@ returns whether @a@ is lesser than or equal to @b@.
(≤) ∷ Ord a ⇒ a → a → Bool
(≤) = (<=)
-- | @a ≥ b@ returns whether @a@ is greater than or equal to @b@.
(≥) ∷ Ord a ⇒ a → a → Bool
(≥) = (>=)
-- | @a × b@ returns @a@ multiplied by @b@.
(×) ∷ Num a ⇒ a → a → a
(×) = (*)
-- | @a ÷ b@ returns @a@ divided by @b@.
(÷) ∷ Fractional a ⇒ a → a → a
(÷) = (/)
-- | @a // b@ returns @a@ floor divided by @b@.
(//) ∷ Integral a ⇒ a → a → a
(//) = div
-- | @a % b@ returns @a@ modulo @b@.
(%) ∷ Integral a ⇒ a → a → a
(%) = mod
-- | @a ⋏ b@ returns the bitwise AND of @a@ and @b@.
(⋏) ∷ Int → Int → Int
(⋏) = (.&.)
-- | @a ⋏ b@ returns the bitwise OR of @a@ and @b@.
(⋎) ∷ Int → Int → Int
(⋎) = (.|.)
-- | @a ≪ b@ returns @a@ with its bits shifted left by @b@ places.
(≪) ∷ Int → Int → Int
(≪) = shiftL
-- | @a ≫ b@ returns @a@ with its bits shifted right by @b@ places.
(≫) ∷ Int → Int → Int
(≫) = shiftR
-- | @a ⊻ b@ returns the bitwise XOR of @a@ and @b@.
(⊻) ∷ Int → Int → Int
(⊻) = xor
-- | @a ∣ b@ returns whether @a@ evenly divides @b@, or @b % a = 0@.
(∣) ∷ Integral a ⇒ a → a → Bool
a ∣ b = b % a ≡ 0
-- | @a ∤ b@ returns whether @a@ does not evenly divide @b@, or @b % a ≠ 0@.
(∤) ∷ Integral a ⇒ a → a → Bool
(∤) = (not ∘) ∘ (∣)
-- | @a … b@ returns a range from @a@ to @b@.
(…) ∷ (Enum a, Ord a) ⇒ a → a → [a]
a … b | b ≥ a = [a .. b]
| otherwise = [b .. a]
-- | @π@ is a floating-point representation of pi.
π ∷ Floating a ⇒ a
π = pi
-- | @f ∘ g@ returns @f@ composed with @g@.
(∘) ∷ (b → c) → (a → b) → a → c
(∘) = (.)
-- | @xs ⊺ xss@ inserts @xs@ in between the lists in @xss@ and concatenates the
-- result.
(⊺) ∷ [a] → [[a]] → [a]
(⊺) = intercalate
-- | @xs ⋅ ys@ returns @xs@ concatenated with @ys@.
(⋅) ∷ [a] → [a] → [a]
(⋅) = (++)
-- | @xs ‼ n@ returns the @n@th element (0-indexed) of @xs@.
(‼) ∷ [a] → Int → a
(‼) = (!!)
-- | @f § x@ returns @f@ mapped over @x@.
(§) ∷ Functor f ⇒ (a → b) → f a → f b
(§) = fmap
-- | @x ∈ xs@ returns whether @x@ is an element of @xs@.
(∈) ∷ (Foldable t, Eq a) ⇒ a → t a → Bool
(∈) = elem
-- | @x ∉ xs@ returns whether @x@ is not an element of @xs@.
(∉) ∷ (Foldable t, Eq a) ⇒ a → t a → Bool
(∉) = notElem
-- | @xs ∋ x@ returns whether @xs@ contains @x@.
(∋) ∷ (Foldable t, Eq a) ⇒ t a → a → Bool
(∋) = flip (∈)
-- | @xs ∌ x@ returns whether @xs@ doesn't contain @x@.
(∌) ∷ (Foldable t, Eq a) ⇒ t a → a → Bool
(∌) = flip (∉)
-- | @xs ⊗ ys@ returns the cartesian product of @xs@ and @ys@.
(⊗) ∷ [a] → [b] → [(a, b)]
xs ⊗ ys = [(x, y) | x ← xs, y ← ys]
-- | @xs ∩ ys@ returns the intersection of @xs@ and @ys@.
(∩) ∷ Eq a ⇒ [a] → [a] → [a]
(∩) = intersect
-- | @xs ∪ ys@ returns the union of @xs@ and @ys@.
(∪) ∷ Eq a ⇒ [a] → [a] → [a]
(∪) = union
-- | @xs ∖ ys@ returns the set minus of @xs@ and @ys@.
(∖) ∷ Eq a ⇒ [a] → [a] → [a]
(∖) = (\\)
-- | @xs ⊆ ys@ returns whether @xs@ is a subset of or equal to @ys@.
(⊆) ∷ Eq a ⇒ [a] → [a] → Bool
(⊆) = isSubsequenceOf
-- | @xs ⊇ ys@ returns whether @xs@ is a superset of or equal to @ys@.
(⊇) ∷ Eq a ⇒ [a] → [a] → Bool
(⊇) = flip (⊆)
-- | @xs ⊈ ys@ returns whether @xs@ is not a subset of or equal to @ys@.
(⊈) ∷ Eq a ⇒ [a] → [a] → Bool
(⊈) = (not ∘) ∘ (⊆)
-- | @xs ⊉ ys@ returns whether @xs@ is not a superset of or equal to @ys@.
(⊉) ∷ Eq a ⇒ [a] → [a] → Bool
(⊉) = flip (⊈)
-- | @xs ⊂ ys@ returns whether @xs@ is a subset of @ys@.
(⊂) ∷ Eq a ⇒ [a] → [a] → Bool
a ⊂ b = (a ⊆ b) ∧ (a ≠ b)
-- | @xs ⊃ ys@ returns whether @xs@ is a superset of @ys@.
(⊃) ∷ Eq a ⇒ [a] → [a] → Bool
(⊃) = flip (⊂)
-- | @xs ⊄ ys@ returns whether @xs@ is not a subset of @ys@.
(⊄) ∷ Eq a ⇒ [a] → [a] → Bool
(⊄) = (not ∘) ∘ (⊂)
-- | @xs ⊅ ys@ returns whether @xs@ is not a superset of @ys@.
(⊅) ∷ Eq a ⇒ [a] → [a] → Bool
(⊅) = flip (⊄)
divisors ∷ Integral a ⇒ a → [a]
divisors n = filter (∣ n) [1..n]
-- | @factors n@ returns the prime factors of @n@.
factors ∷ Integral a ⇒ a → [a]
factors n
| isPrime n = [n]
| otherwise = i : factors (n // i)
where i = find (∣ n) (tail primes)
-- | @isPrime n@ returns whether @n@ is prime.
isPrime ∷ Integral a ⇒ a → Bool
isPrime 2 = True
isPrime 3 = True
isPrime n
| (2 ∣ n) ∨ (3 ∣ n) = False
| otherwise = check 5 2
where check i w
| i × i > n = True
| i ∣ n = False
| otherwise = check (i + w) (6 - w)
-- | @primes@ is an infinite list of prime numbers.
primes ∷ Integral a ⇒ [a]
primes = filter isPrime [1..]
-- | @prime n@ returns the @n@th (0-indexed) prime number.
prime ∷ Integral a ⇒ Int → a
prime = (primes ‼)
-- | @fibList@ is an infinite list of Fibonacci numbers (@[0, 1, 1, 2...]@).
fibList ∷ Integral a ⇒ [a]
fibList = 0 : 1 : zipWith (+) fibList (tail fibList)
-- | @fib n@ returns the @n@th (0-indexed) Fibonacci number.
fib ∷ Integral a ⇒ Int → a
fib = (fibList ‼)
-- | @isFib n@ returns whether @n@ is a Fibonacci number.
isFib ∷ Integral a ⇒ a → Bool
isFib n = find (≥ n) fibList ≡ n
-- | @deltas xs@ returns the incremental differences of @xs@.
deltas ∷ Num a ⇒ [a] → [a]
deltas l = zipWith (-) (tail l) l
-- | @find p xs@ finds the first element of @xs@ for which @p@ holds true.
find ∷ (a → Bool) → [a] → a
find p xs = head $ dropWhile (not ∘ p) xs
-- | @bitwiseNot n@ performs bitwise NOT on @n@, or returns @n@'s bitwise
-- complement.
bitwiseNot ∷ Int → Int
bitwiseNot = complement
-- | @decToBin n@ converts @n@, which represents a decimal number, to a string
-- representing @n@ in binary.
decToBin ∷ (Integral a, Show a) ⇒ a → String
decToBin 0 = "0"
decToBin 1 = "1"
decToBin n = decToBin (n // 2) ⋅ show (n % 2)
-- | @binToDec x@ converts @x@, which represents a binary number, to an
-- integral value representing @x@ in decimal.
binToDec ∷ (Integral a, Read a) ⇒ String → a
binToDec "" = 0
binToDec x = 2 × binToDec (init x) + read [last x]