[Haskell-cafe] Combine list of sorted lists
quancta at gmail.com
Fri Dec 29 14:26:24 EST 2006
On 12/29/06, Neil Mitchell <ndmitchell at gmail.com> wrote:
> > f1 :: [Int] -> [[Int]]
> > f1  = 
> > f1 (a:as) = [a] : f1 as
> f1 is simply a map
> > f3 la lb = let a = head la
> > b = head lb
> > in if sum a <= sum b then
> > a : f3 (tail la) lb
> > else
> > b : f3 la (tail lb)
> Why not use pattern matching to split up la and lb, rather than head/tail?
> I would have thought the whole function could be written as a nice
> foldr merge, where merge :: [Int] -> [Int] -> [Int]. Thats only a
> guess at the top of my head though, not worked out properly.
> Is this homework? If so its useful to state when you post the question :)
I am not sure how to express f1 with map? how do I say
(map (lambda (x) (list x))
in Haskell? map () ?
Here is my new f3:
f3 :: [[Int]] -> [[Int]] -> [[Int]]
f3  lb = lb
f3 la  = la
f3 la@(a:as) lb@(b:bs) = if sum a <= sum b then
a : f3 as lb
b : f3 la bs
(btw, yes this is homework assigned by prof. Quan in quantum computing class
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe