[Haskell-cafe] Combine list of sorted lists
Quan Ta
quancta at gmail.com
Fri Dec 29 14:26:24 EST 2006
On 12/29/06, Neil Mitchell <ndmitchell at gmail.com> wrote:
>
> Hi
>
> > 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 :)
>
>
Hi Neil,
I am not sure how to express f1 with map? how do I say
(lambda (ls)
(map (lambda (x) (list x))
ls))
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
else
b : f3 la bs
(btw, yes this is homework assigned by prof. Quan in quantum computing class
:))
Thanks,
Quan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20061229/79b135ec/attachment.htm
More information about the Haskell-Cafe
mailing list