Cantor set: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
m →‎{{header|AppleScript}}: tidied. Added preamble.
Hout (talk | contribs)
→‎Haskell :: Dual representation: Tidied, pruned out one import.
Line 918:
<lang haskell>import Data.Ratio (Ratio, (%), numerator, denominator)
import Data.List (intercalate, mapAccumL, maximumBy)
import Data.Bool (bool)
 
--------------------------- CANTOR -------------------------
cantor :: (Rational, Rational) -> [[(Rational, Rational)]]
cantor = iterate (>>= go) . return
where
let go (x, y) =
go (x, y) = let[(x, rx =+ r), (y - x)r, / 3y)]
0where
in [(x, x + r), (y - r, y)]
in iterate (>> r = go(y - x) ./ return3
 
---------------------------- TEST --------------------------
main :: IO ()
main = do
Line 933 ⟶ 934:
putStrLn $ intervalBars xs
 
-- DISPLAY FUNCTIONS -------------------------- DISPLAY -------------------------
intervalBars :: [[(Rational, Rational)]] -> String
intervalBars xs = unlines $ go (d % 1) <$> xs
where
let go w xs =
d = maximum $ concatdenominator . sndfst <$> last xs
go w xs mapAccumL=
concat . snd (\a (rx, ry) ->$
mapAccumL
let (wx, wy) = (w * rx, w * ry)
in(\a [(xrx, x + rry), (y - r, y)]>
in ( wy -- Accumulator – end of previous interval.
let (wx, replicatewy) (floor= (wxw -* a))rx, 'w '* -- Preceding gap, andry)
in ( wy -- Accumulator end of ++previous interval.
, replicate (floor (wywx - wxa)) ' ' -- intervalPreceding gap, bar.and
))++
replicate (floor (wy - wx)) '█' -- interval bar.
0
xs ))
0
d = maximum $ (denominator . fst) <$> last xs
in unlines $ go (d % 1) <$> xs
 
intervalRatios :: [(Rational, Rational)] -> String
intervalRatios xs = ('(' :) . (++ ")") . intercalate ") (" . fmap go
where
let go (rx, ry) = intercalate ", " $ showRatio <$> [rx, ry]
in ' go ('rx, :ry) = intercalate "), (" (go$ showRatio <$> xs)[rx, ++ ")"ry]
 
showRatio :: Rational -> String
showRatio = ((++) . show . numerator) <*> (go . denominator)
showRatio r =
where
let d = denominator r
let go (x, y) =
in show (numerator r) ++ bool [] ('/' : show d) (1 /= d)</lang>
| 1 /= x = '/' : show x
| otherwise = []</lang>
{{Out}}
<pre>(0, 1)