[Haskell-beginners] merge error

Alex Rozenshteyn rpglover64 at gmail.com
Fri May 18 05:28:23 UTC 2018


If you compile with -Wall, you get the following

foo.hs:2:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘merge’:
        Patterns not matched:
            [] (_:_:_)
            (_:_:_) []
  |
2 | merge [] [] = []
  | ^^^^^^^^^^^^^^^^...

That is to say, you never match if there is an empty list and a list of 2
or more.

Try this:

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


On Thu, May 17, 2018 at 10:21 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/20180517/e58804f3/attachment.html>


More information about the Beginners mailing list