[Haskell-cafe] Behavior of groupBy

Henning Thielemann lemming at henning-thielemann.de
Mon May 21 06:17:24 EDT 2007


On Sat, 28 Apr 2007, Hans van Thiel wrote:

> Hello All,
>
> The standard function groupBy of List.hs doesn't work as I expect in
> this case:
>
> groupBy (\x y -> (last x) == (last y)) ["abc", "bd","cac"]
>
> results in:
>
> [["abc"],["bd"],["cac"]]
>
> where I want:
>
> [["abc","cac"], ["bd"]]

I think you must roll your own one. How about repeated 'partition' to
extract all elements that have the same trailing character like the head
word of the list?

I have done this in a more complex setting and called it 'slice':
  http://darcs.haskell.org/haskore/src/Haskore/Basic/TimeOrderedList.lhs


> slice :: (Eq a, Num time) =>
>    (body -> a) -> T time body -> [(a, T time body)]
> slice f perf =
>   let splitByHeadKey pf =
>          fmap
>             (\ev ->
>                let i = f (eventBody ev)
>                    (pf0, pf1) = partition ((i==) . f) 0 0 pf
>                in  ((i,pf0), pf1))
>             (listToMaybe pf)
>   in  List.unfoldr splitByHeadKey perf



More information about the Haskell-Cafe mailing list