[noob] Typing error with nested list and tuple pattern?
Hi all I am baffled by the following error message from Hugs 98 (Version May 2006): ERROR file:.\cube.hs:11 - Type error in explicitly typed binding *** Term : [(i,v) : ps] *** Type : [[(a,b)]] *** Does not match : [(Int,Double)] when attempting to compile the following declaration (line numbers in brackets): [10] test :: [(Int, Double)] -> Int [11] test [(i,v):ps] = i Is this a bug? If not, how do I write the pattern properly? Thanks, Peter Arrenbrecht -- View this message in context: http://www.nabble.com/-noob--Typing-error-with-nested-list-and-tuple-pattern... Sent from the Haskell - Hugs-Users forum at Nabble.com.
Hello Peter, Thursday, September 7, 2006, 11:02:12 AM, you wrote:
[10] test :: [(Int, Double)] -> Int [11] test [(i,v):ps] = i
test ((i,v):ps) = i your mistake was using for grouping [] instead of () [] should be used only with ',' or '..': [1,2,3] [(1,0.7), (2,0.8)] [1..10] (x:xs) means constructing list from element representing head of new list and old list representing all remaining elements [x:xs] is equivalent to [(x:xs)] and means constructing singleton (one-element) list which only element is another list, namely (x:xs). so, you got list of lists ps: btw, it's better to ask newbie questions in haskell-cafe list -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
Peter Arrenbrecht
ERROR file:.\cube.hs:11 - Type error in explicitly typed binding *** Term : [(i,v) : ps] *** Type : [[(a,b)]] *** Does not match : [(Int,Double)]
[10] test :: [(Int, Double)] -> Int [11] test [(i,v):ps] = i
The error message does contain the answer. Your pattern [(i,v):ps] represents a singleton list containing a list of pairs. That is a list of lists. What the explicit type signature reveals to be your intention was a single-level list of pairs. To fix, use round parens around the pattern, not square brackets: ((i,v):ps) Regards, Malcolm
Am Donnerstag, 7. September 2006 09:02 schrieb Peter Arrenbrecht:
Hi all
I am baffled by the following error message from Hugs 98 (Version May 2006):
ERROR file:.\cube.hs:11 - Type error in explicitly typed binding *** Term : [(i,v) : ps] *** Type : [[(a,b)]] *** Does not match : [(Int,Double)]
when attempting to compile the following declaration (line numbers in brackets):
[10] test :: [(Int, Double)] -> Int [11] test [(i,v):ps] = i
Is this a bug? If not, how do I write the pattern properly?
No bug. You should write test ((i,v):ps) = i The pattern [(i,v):ps] has type list of lists of (Int,Double) pairs, one more layer of list than you want.
Thanks, Peter Arrenbrecht
You're welcome, Daniel -- "In My Egotistical Opinion, most people's C programs should be indented six feet downward and covered with dirt." -- Blair P. Houghton
participants (5)
-
Bulat Ziganshin -
Daniel Fischer -
Malcolm Wallace -
Neil Mitchell -
Peter Arrenbrecht