[Haskell-cafe] Splitting a list
Joe Fasel
jhf at lanl.gov
Thu Apr 22 16:38:07 EDT 2004
On 2004.04.22 15:02, I wrote:
> splitAll :: (Real a) => a -> [a] -> [[a]]
> splitAll = unfoldr . split
> where split _ [] = Nothing
> split n xs = let (ys,zs) = break ((> n) . snd)
> (zip xs (scanl1 (+) xs))
> in Just (map fst ys, map fst zs)
a slight improvement:
splitAll :: (Real a) => a -> [a] -> [[a]]
splitAll n = unfoldr split
where split [] = Nothing
split xs = let (ys,zs) = break ((> n) . snd)
(zip xs (scanl1 (+) xs))
in Just (map fst ys, map fst zs)
But in fact, I think you can do better still by not holding n
constant but using a higher threshold on each split and not
projecting out the values of the second component, thus only
zipping the whole list once.
--Joe
Joseph H. Fasel, Ph.D. email: jhf at lanl.gov
Systems Planning and Analysis phone: +1 505 667 7158
University of California fax: +1 505 667 2960
Los Alamos National Laboratory post: D-2 MS F609; Los Alamos, NM 87545
More information about the Haskell-Cafe
mailing list