summaryrefslogtreecommitdiff
path: root/SmallRing.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-05-24 21:11:14 -0400
committerJoe Crayne <joe@jerkface.net>2019-05-24 21:11:14 -0400
commit786e58ccbb05ced78c5421b53fbc469971d7db82 (patch)
tree0c60c0bbfd357c3d21d849dd61ecfb50d7ac1cc7 /SmallRing.hs
parenta2aa65ffc5a4fc6cd0f41ccab1516f85a27989b2 (diff)
Add curvature-coloring.
Diffstat (limited to 'SmallRing.hs')
-rw-r--r--SmallRing.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/SmallRing.hs b/SmallRing.hs
new file mode 100644
index 0000000..d792899
--- /dev/null
+++ b/SmallRing.hs
@@ -0,0 +1,39 @@
1{-# LANGUAGE RankNTypes #-}
2module SmallRing where
3
4import Data.Ratio
5
6odds :: (Integral i, Num a) => i -> [a]
7odds x = [ 1 + 2 * fromIntegral n | n <- [0..x] ]
8
9pts :: (Fractional a, Integral b) => b -> [a]
10pts i = let d = 2^i in [ k / d | k <- odds (2^(i-1) - 1) ]
11
12fractions :: Fractional b => [b]
13fractions = concatMap pts [1..]
14
15vis :: Int -> Ratio Int -> [Char]
16vis w α = let i = numerator α
17 n = denominator α
18 x = (w * i) `div` n
19 in replicate (x-1) '.' ++ "x" ++ replicate (w-max x 1) '.' ++ show α
20
21data Giver a
22 = Give0
23 | Give1 (forall b. (a -> b) -> b)
24 | Give2 (forall b. (a -> a -> b) -> b)
25 | Give3 (forall b. (a -> a -> a -> b) -> b)
26
27pushFront :: a -> Giver a -> Giver a
28pushFront a Give0 = Give1 ($ a)
29pushFront a (Give1 f) = Give2 (\g -> f (g a))
30pushFront a (Give2 f) = Give3 (\g -> f (g a))
31pushFront a (Give3 f) = f $ \b c _ -> Give3 $ \g -> g a b c
32
33take3 :: (a -> a -> a -> b) -> Giver a -> Maybe b
34take3 f (Give3 g) = Just (g f)
35take3 _ _ = Nothing
36
37with3 :: Applicative f => Giver a -> (a -> a -> a -> f ()) -> f ()
38with3 (Give3 g) f = g f
39with3 _ _ = pure ()