[Haskell-cafe] Computing a sorted list of products lazily
Daniel Fischer
daniel.is.fischer at web.de
Fri Apr 17 18:38:11 EDT 2009
Am Samstag 18 April 2009 00:31:50 schrieb Martijn van Steenbergen:
> Hi Daniel,
>
> I love your solution. Very elegant.
>
> Daniel Fischer wrote:
> > sortedProducts xs ys = foldr merge1 [] [map (*x) ys | x <- xs]
> >
> > merge1 (x:xs) ys = x:merge xs ys
> > merge1 [] ys = ys
> >
> > merge xs@(x:xt) ys@(y:yt)
> >
> > | x < y = x:merge xt ys
> > | otherwise = y:merge xs yt
> >
> > (or remove duplicates, if you wish)
>
> Small addition: if you want this to run on finite input as well, add
>
> this case:
> > merge xs ys = xs ++ ys
Argh! When typing the foldr, I thought I mustn't forget the case for an empty list in
merge1 and merge. Well, at least I remembered half of it :(
>
> Martijn.
More information about the Haskell-Cafe
mailing list