merge first second | (head first) <= (head second) = (head first) : merge (tail first) second
| otherwise = (head second): merge first (tail second)
merge first@(x:xs) second@(y:ys) | x <= y = x : merge xs second
| otherwise = y : merge first ys
Thanks to all. I used Mukesh's suggestion.I am still not clear on:why [x] /= xswhy first == first@(x:xs), especially weather the variable declarations are considered names for the same thing._______________________________________________On Thu, May 17, 2018 at 10:39 PM Hemanth Gunda <hemanth.420@gmail.com> wrote:Hi Trent,This works:merge:: Ord a => [a] -> [a] -> [a]merge [] [] = []merge x [] = xmerge [] y = ymerge first@(x:xs) second@(y:ys)| x <= y = x : merge xs second| otherwise = y : merge first ysDifference in the linesmerge x [] = xmerge [] y = yAs 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, HemanthOn Fri, May 18, 2018 at 10:51 AM trent shipley <trent.shipley@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@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners