
17 Apr
2009
17 Apr
'09
6:38 p.m.
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.