Stem-and-leaf plot: Difference between revisions
→{{header|Haskell}}
Line 1,015:
-- Re-reading the initial strings as Ints
-- (empty strings read as 0),
ns :: [(Int, Char)]
ns =
let s = fst
in ( if null
, snd x)) <$>
-- and sorting and grouping by these initial Ints,
-- interpreting them as data-collection bins.
bins :: [[(Int, Char)]]
bins =
groupBy (on (==) fst)
-- Forming bars by the ordered accumulation of final characters in each bin,
bars :: [(Int, String)]
bars
-- and obtaining a complete series, with empty bar strings
-- interpolated for any missing integers.
series :: [(Int, String)]
series =
(concat . snd) $
mapAccumL
(\a x ->
let n = fst x
in
then (a + 1, [x])
else (n + 1, ((\i -> (i, "")) <$> [a .. (n - 1)]) ++ [x]))
1
bars
-- Assembling the series as a list of strings with right-justified indices,
justifyRight :: Int -> Char -> String -> String
justifyRight n c s = drop (length s) (replicate n c ++ s)
plotLines :: [String]
plotLines =
foldr
▲ \x a -> (
(\x a ->
(justifyRight 2 ' ' (show (fst x)) ++ " | " ++
[]
series
-- and passing these over to IO as a single newline-delimited string.
main :: IO ()
main = putStrLn $ unlines plotLines</lang>
|