Couldn't match expected type `a' with actual type `[a]'

前端 未结 3 2244
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 10:14

I was able to execute the following code flawlessly

myLast :: [a] -> a
myLast [] = error \"Can\'t call myLast on an empty list!\"
myLast (x:_) = x
         


        
3条回答
  •  执念已碎
    2021-02-14 10:40

    (_:x) matches _ with the head and x with the tail of the list. The type of tail of a list is [a]. You are trying to return [a]' where as the function declaration specifies return type as a.

    myLast (_:x) = x
    

    If you want to match last element take a look at this answer - Can you use pattern matching to bind the last element of a list?

提交回复
热议问题