我正在尝试编写一个检索列表中第 n 个元素的函数。
这是我到目前为止所拥有的:
main :: IO ()
main = do print (nth 3 [1,2,10])
nth _ [] = []
nth a (x:xs)
| (a == 1) = x
| otherwise = nth (a-1) xs
这是我得到的错误:
Error occurred
ERROR line 2 - Cannot justify constraints in explicitly typed binding
*** Expression : main
*** Type : IO ()
*** Given context : ()
*** Constraints : (Show a, Num [a])