【发布时间】:2023-03-29 16:58:01
【问题描述】:
如果我想在我的函数中访问/引用一个字符参数,我只是这样做:
myFunc :: Char -> Bool
myFunc c = ... --Here I use 'c' to work with my Char parameter
使用列表:
myList :: [Int] -> [Int]
myList l = ... --I can access the list with 'l'.
现在,我如何访问/工作/引用列表中的元组数据:
myFunc :: [(Int, Int)] -> [(Int, Int)] --Receive a list of tuple as parameter
myFunc ?? = ... --How may I access the tuple element ? How may I access its data. Just use a letter there and try to work with `fst` or `snd` does not work.
更新
myFunc :: [(Int, Int)] -> [(Int, Int)]
myFunc [] = [(0, 0)]
myFunc ((x,y):rest) = x --Error here:
Couldn't match expected type `[(Int, Int)]' with actual type `Int' In the expression: x
【问题讨论】:
-
您收到错误是因为
x是Int类型的值。您的类型签名表明您的函数正在返回[(Int, Int)]类型的值,但您实际上正在返回Int并因此返回错误。