[Haskell-cafe] Pattern matching error

2007-12-06 Thread georg86

Hello!
I need to write a function which should is supposed to merge multiple
entries with the same
key (out of a sorted key-value-list) into one single entry.
However, I keep getting a pattern matching error.

(For example, for input [('b',[5]),('b',[4]),('b',[1]),('b',[6])]: 
Program error: pattern match failure: kgroup
[('b',[5,4]),('b',[1]),('b',[6])]) 

Thank you very much for your help.

kmerge::Eq a = [(a,[b])]-[(a,[b])]

kmerge [] = []

kmerge ((x,[y]):[]) = [(x,[y])]

kmerge ((x,[y]):(u,[v]):xs) | x == u = kmerge ((x,[y,v]):xs) 
 | otherwise = (x,[y]):(u,[v]):kmerge xs
-- 
View this message in context: 
http://www.nabble.com/Pattern-matching-error-tf4957268.html#a14196413
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Pattern matching error

2007-12-06 Thread Brent Yorgey
 kmerge ((x,[y]):[]) = [(x,[y])]


Matching on [y] like this will only match lists with a single element (and
bind y to that element).  You probably just want to say

kmerge ((x,y):[]) = [(x,y)]

etc., which will bind y to the entire list.

-Brent
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Pattern matching error

2007-12-06 Thread Philip Weaver
If you add a third pattern, you can see exactly what it's failing to match:

kmerge x = error (show x)

In order to do this, you just need to add Show constraints for a and b in
the type of kmerge:

   kmerge :: (Show a, Show b, Eq a) = [(a,[b])]-[(a,[b])]

You'll find that the pattern that it's failing to match is:

   [('b',[5,4]),('b',[1]),('b',[6])]

Because you combined 5 and 4 and passed that on to your recursive 'kmerge'
call.  Your patterns only cover the case where there is exactly one element
in the list.

- Phil

On Dec 6, 2007 9:39 AM, georg86 [EMAIL PROTECTED] wrote:


 Hello!
 I need to write a function which should is supposed to merge multiple
 entries with the same
 key (out of a sorted key-value-list) into one single entry.
 However, I keep getting a pattern matching error.

 (For example, for input [('b',[5]),('b',[4]),('b',[1]),('b',[6])]:
 Program error: pattern match failure: kgroup
 [('b',[5,4]),('b',[1]),('b',[6])])

 Thank you very much for your help.

 kmerge::Eq a = [(a,[b])]-[(a,[b])]

 kmerge [] = []

 kmerge ((x,[y]):[]) = [(x,[y])]

 kmerge ((x,[y]):(u,[v]):xs) | x == u = kmerge ((x,[y,v]):xs)
 | otherwise = (x,[y]):(u,[v]):kmerge
 xs
 --
 View this message in context:
 http://www.nabble.com/Pattern-matching-error-tf4957268.html#a14196413
 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Pattern matching error

2007-12-06 Thread Ketil Malde
Philip Weaver [EMAIL PROTECTED] writes:

 You'll find that the pattern that it's failing to match is:

[('b',[5,4]),('b',[1]),('b',[6])]

You could also use ghc with -Wall, which will tell you exactly which
cases you've omitted.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe