Problem Description
I have used the开发者_如何学Go following code to to access list of tuples im not getting why im getting this error is ...
also i would like to know if i provided the execas
mn::[(1,2,3),(6,7,8)]
how can i acess the(6,7,8)
?
Pure Function
type rty= [(Int,Int,Int)]
mn::rty->Int
mn (a,b,c) = a
Error Message
Type error in explicitly typed binding
*** Term : (a,b,c)
*** Type : (a,b,c)
*** Does not match : rty
rty is the same as [(a, b, c)]
, a list. You are matching against a tuple instead of a list. Maybe what you want is:
mn [] = ?
mn ((a, b, c):xs) = ?
The pattern
(a,b,c)
matches triples, not lists of triples (rty
).If you want to access the second element in a list of triples, you'll have to use some kind of recursive solution (explicit or through a library function). It rather depends on what
mn
is supposed to do.
You are trying to match a single tuple, but not a list of tuples. I'd suggest you to change the type:
mn :: (Int,Int,Int) -> Int
mn (a,_,_) = a
To access the n-th element of a list, use (!!)
like in xs !! 2
. Please note, that the function is not safe. This is, if the list is too short, an exception is thrown.
type rty = [(Int,Int,Int)]
mn ::rty -> Int
mn ((a,b,c):xs) = a
Since rty is a list of tuples, you must get a tuple out of the rty and then return it.
精彩评论