Proposal: Adding on
Duncan Coutts
duncan.coutts at worc.ox.ac.uk
Sat Nov 4 22:17:16 EST 2006
On Sat, 2006-11-04 at 21:25 -0500, Jan-Willem Maessen wrote:
> On Nov 3, 2006, at 11:58 AM, Bulat Ziganshin wrote:
> >
> > while we are here, i also propose to export from Data.List 'merge'
> > function that is useful on its own
>
> Yes, please. This qualifies as my most commonly rewritten multi-line
> Haskell function.
>
> That said, I've also written most of the variations of merge you
> might care to imagine (indeed, sorted-list sets and multisets
> together use pretty much all of them if you include difference and
> merging of equals).
Yes, there are a number of different options for what we might call
'merge'. It would be good to discuss what the variations are and which
variations might be sensible to include in a standard lib and what they
might be called.
However I think this is a separate issue from the discussion about 'on'.
So start a new thread and lets try and find out if there is a consensus.
As an example variation from a program I wrote:
-- mergeBy cmp xs ys = (only_in_xs, in_both, only_in_ys)
mergeBy :: (a -> b -> Ordering) -> [a] -> [b] -> ([a], [(a, b)], [b])
mergeBy cmp = merge [] [] []
where merge l m r [] ys = (reverse l, reverse m, reverse (ys++r))
merge l m r xs [] = (reverse (xs++l), reverse m, reverse r)
merge l m r (x:xs) (y:ys) =
case x `cmp` y of
GT -> merge l m (y:r) (x:xs) ys
EQ -> merge l ((x,y):m) r xs ys
LT -> merge (x:l) m r xs (y:ys)
It's rare of course that you need the generality of merging lists of
different types. Though of course this also covers the slightly more
common case of comparing where == is only an equivalence relation and
you want to keep or combine both elements.
Duncan
More information about the Libraries
mailing list