Cantor set: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
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.Ratio (Ratio, (%), numerator, denominator)
<lang haskell>import Data.List (intercalate, mapAccumL, maximumBy)
import Data.List (intercalate, mapAccumL, maximumBy)
import Data.Ratio (Ratio, denominator, numerator, (%))


--------------------------- CANTOR -------------------------
-------------------------- CANTOR ------------------------


cantor :: (Rational, Rational) -> [[(Rational, Rational)]]
cantor :: (Rational, Rational) -> [[(Rational, Rational)]]
Line 928: Line 928:
r = (y - x) / 3
r = (y - x) / 3


---------------------------- TEST --------------------------
--------------------------- TEST -------------------------


main :: IO ()
main :: IO ()
Line 936: Line 936:
putStrLn $ intervalBars xs
putStrLn $ intervalBars xs


-------------------------- DISPLAY -------------------------
------------------------- 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 (wx - a)) ' '
replicate (floor (wy - wx)) '█'))
<> replicate (floor (wy - wx)) '█'
0
)
xs
)
0
xs


intervalRatios :: [(Rational, Rational)] -> String
intervalRatios :: [(Rational, Rational)] -> String
intervalRatios = ('(' :) . (++ ")") . intercalate ") (" . fmap go
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 = ((++) . show . numerator) <*> (go . denominator)
showRatio = ((<>) . show . numerator) <*> (go . denominator)
where
where
go x
go x