[Haskell-beginners] merge error

mukesh tiwari mukeshtiwari.iiitm at gmail.com
Fri May 18 05:33:12 UTC 2018


I have changed your code little bit, and now it works.

merge :: Ord a => [a] -> [a] -> [a]
merge [] second = second
merge first [] = first
merge first@(x:xs) second@(y:ys)
  | x <= y    = x : merge xs second
  | otherwise = y : merge first ys

The reason your code is not working because
merge [4,5] [] is trying to match it against merge [x] [] = [x] which
expects one element list at first place so merge [1] [] would work,but not
merge (list having more than one element) [].

Best,
Mukesh Tiwari



On Fri, May 18, 2018 at 3:20 PM, trent shipley <trent.shipley at gmail.com>
wrote:

> The below produces an error. And I am very proud that I could use the GHCi
> debugging tools to get this far.
>
> merge [] [] works.
>
> merge [1] [] works.
>
> I don't know why the failing example fails. It should return:
>
> [4,5]
>
> Help to unstuck is appreciated.
>
> :step merge [4,5] []
>
> *** Exception: ex6_8.hs:(12,1)-(16,66): Non-exhaustive patterns in
> function merge
>
> Given:
>
> merge :: Ord a => [a] -> [a] -> [a]
>
> merge [] [] = []
>
> merge [x] [] = [x]
>
> merge [] [y] = [y]
>
> merge first@(x:xs) second@(y:ys) | x <= y     = x : merge xs second
>
>                                  | otherwise  = y : merge first ys
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180518/8c6f5956/attachment.html>


More information about the Beginners mailing list