Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. merge error (trent shipley)
2. Re: merge error (Alex Rozenshteyn)
3. Re: merge error (mukesh tiwari)
4. Re: merge error (Hemanth Gunda)
----------------------------------------------------------------------
Message: 1
Date: Thu, 17 May 2018 22:20:36 -0700
From: trent shipley <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] merge error
Message-ID:
<CAEFLyb+U-=wqdlirx2l9h-kd4qby6p2a+omx2m7pgsz-01x...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20180517/00ecdf79/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 17 May 2018 22:28:23 -0700
From: Alex Rozenshteyn <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] merge error
Message-ID:
<CALm==bx0be0lo5ox_mdixaheu2cx9omqi1gmcvnialoehjo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]>
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
> [email protected]
> 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-0001.html>
------------------------------
Message: 3
Date: Fri, 18 May 2018 15:33:12 +1000
From: mukesh tiwari <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] merge error
Message-ID:
<CAFHZvE8WzQ+Ntxs0QTa6JAmt=fnic1uqkqe7nho63btu8d9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]>
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
> [email protected]
> 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-0001.html>
------------------------------
Message: 4
Date: Fri, 18 May 2018 11:08:46 +0530
From: Hemanth Gunda <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] merge error
Message-ID:
<CAJMdXuNLP1Q=5f8h4v_aysdwzxms2pywx4mj5wbhszpjflv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi Trent,
This works:
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
Difference in the lines
merge x [] = x
merge [] y = y
As the input is of type [a] where a belongs to typeclass Ord, you must pass
x instead of [x].
[x] would work if you tried merge [4] []. but will fail if you tried merge
[4,5] []. because "4,5" isn't of type a.
Regards, Hemanth
On Fri, May 18, 2018 at 10:51 AM trent shipley <[email protected]>
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
> [email protected]
> 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/c533c0f3/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 119, Issue 9
*****************************************