Bin given limits: Difference between revisions

m
→‎{{header|Haskell}}: added description
(→‎{{header|Haskell}}: changed to simpler and more efficient monadic solution)
m (→‎{{header|Haskell}}: added description)
Line 758:
 
=={{header|Haskell}}==
 
<lang haskell>import Text.Printf
Splitting the data into bins is done using the monadic nature of a tuple.
import Control.Monad (foldM)
Here tuple plays role of the Writer monad, so that sequential partitioning by each bin boundary
adds new bin contents.
 
<lang haskell>import TextControl.PrintfMonad (foldM)
import Data.List (partition)
 
Line 769 ⟶ 773:
 
binCounts :: Ord a => [a] -> [a] -> [Int]
binCounts b = fmap length . binSplit b</lang>
</lang>
 
<pre>λ> binSplit [2,4,7] [1,4,2,6,3,8,9,4,1,2,7,4,1,5,1]
Line 780 ⟶ 783:
Tasks examples
 
<lang haskell>taskimport bs ns = mapM_ putStrLnText.Printf
 
task bs ns = mapM_ putStrLn
$ zipWith mkLine (binCounts bs ns) bins
where
Anonymous user