Cantor set: Difference between revisions
Content deleted Content added
m →Haskell :: Cantor Interval bars: Tidied |
m →Haskell :: Dual representation: (Tidying, applying Ormolu) |
||
Line 916: | Line 916: | ||
===Dual representation=== |
===Dual representation=== |
||
Intervals as fraction pairs, and intervals as graphic bars: |
Intervals as fraction pairs, and intervals as graphic bars: |
||
<lang haskell>import Data. |
<lang haskell>import Data.List (intercalate, mapAccumL, maximumBy) |
||
import Data. |
import Data.Ratio (Ratio, denominator, numerator, (%)) |
||
-------------------------- CANTOR ------------------------ |
|||
cantor :: (Rational, Rational) -> [[(Rational, Rational)]] |
cantor :: (Rational, Rational) -> [[(Rational, Rational)]] |
||
Line 928: | Line 928: | ||
r = (y - x) / 3 |
r = (y - x) / 3 |
||
--------------------------- TEST ------------------------- |
|||
main :: IO () |
main :: IO () |
||
Line 936: | Line 936: | ||
putStrLn $ intervalBars xs |
putStrLn $ intervalBars xs |
||
------------------------- DISPLAY ------------------------ |
|||
intervalBars :: [[(Rational, Rational)]] -> String |
intervalBars :: [[(Rational, Rational)]] -> String |
||
Line 944: | Line 944: | ||
go w xs = |
go w xs = |
||
concat . snd $ |
concat . snd $ |
||
mapAccumL |
mapAccumL |
||
(\a (rx, ry) -> |
( \a (rx, ry) -> |
||
let (wy, wx) = (w * ry, w * rx) |
let (wy, wx) = (w * ry, w * rx) |
||
in ( wy |
in ( wy, |
||
replicate (floor (wx - a)) ' ' |
|||
replicate (floor (wy - wx)) '█' |
<> replicate (floor (wy - wx)) '█' |
||
) |
|||
) |
|||
0 |
|||
xs |
|||
intervalRatios :: [(Rational, Rational)] -> String |
intervalRatios :: [(Rational, Rational)] -> String |
||
intervalRatios = |
intervalRatios = |
||
('(' :) . (<> ")") |
|||
. intercalate ") (" |
|||
. fmap go |
|||
where |
where |
||
go (rx, ry) = intercalate ", " $ showRatio <$> [rx, ry] |
go (rx, ry) = intercalate ", " $ showRatio <$> [rx, ry] |
||
showRatio :: Rational -> String |
showRatio :: Rational -> String |
||
showRatio = (( |
showRatio = ((<>) . show . numerator) <*> (go . denominator) |
||
where |
where |
||
go x |
go x |