开发者

explicitly typed binding problem in Haskell

开发者 https://www.devze.com 2023-03-09 18:51 出处:网络
Problem Description I have used the开发者_如何学Go following code to to access list of tuples im not

Problem Description

  1. I have used the开发者_如何学Go following code to to access list of tuples im not getting why im getting this error is ...

  2. 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) = ?


  1. The pattern (a,b,c) matches triples, not lists of triples (rty).

  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消