Anonymous user
Bin given limits: Difference between revisions
m
→{{header|Haskell}}: formatting issues
(→{{header|Haskell}}: added binary search solution) |
m (→{{header|Haskell}}: formatting issues) |
||
Line 759:
=={{header|Haskell}}==
Splitting the data into bins may be done using the monadic nature of a tuple. Here tuple plays role of the Writer monad, so that sequential partitioning by each bin boundary adds new bin contents.
<lang haskell>import Control.Monad (foldM)
Line 796 ⟶ 794:
mkTree lst = Node x (mkTree l) (mkTree r)
where (l, x:r) = splitAt (length lst `div` 2) lst
add x (Val v) = Val (x:v)▼
add x (Node y l r) = if x < y▼
then Node y (add x l) r▼
else Node y l (add x r)▼
binSplit :: Ord a => [a] -> [a] -> [[a]]
binSplit lims = toList . foldr add (mkTree lims)
where
▲ add x (Val v) = Val (x:v)
▲ add x (Node y l r) = if x < y
▲ then Node y (add x l) r
▲ else Node y l (add x r)</lang>
Tasks examples
|