问题
I am confused as to why the 1st and 3rd versions of this functions give this error whereas the second definition works fine.
-- head and tail
third :: [a] -> a
third [a] = head (tail (tail[a]))
-- Pattern matching
third2 :: [a] -> a
third2 (_:_:x:_) = x
-- List indexing
third3 :: [a] -> a
third3 [a] = [a]!!2
Thanks in advance
回答1:
That is odd that the second one does not complain about non-exhaustive
patterns, since third2
will not match lists of length zero, one, or two.
The third
and third3
functions complain because [a]
is not a variable,
it is a pattern. [a]
desugars to (a:[])
, so you could have written them as
third (a:[]) = head (tail (a:[]))
third3 (a:[]) = (a:[]) !! 2
Neither of which will work, as those are single element lists. I suspect what you want is
third a = head (tail a)
third3 a = a !! 2
回答2:
You need to understand the syntax better.
Basically, there are 2 sub-syntaxes:
- syntax for types
- syntax for expressions and patterns
In the type syntax, [a]
means list of elements of type a
In the expression/pattern syntax, [a]
means the singleton list, that contains the value a
. This is equivalent to (a:[])
(a prepended to the empty list).
Hence your first function, for example, checks if it gets a singleton list. Then it takes the head of the tail of the tail of a singleton list, which will fail.
The message you're getting is because there are shapes of lists you didn't cover: namely, the empty list and lists with more than 1 element.
And, of course, you should be getting a warning for third2
, is it only covers lists with 3 or more elements. I am sure you're overlooking something.
来源:https://stackoverflow.com/questions/14897529/haskell-non-exhaustive-patterns-in-function-simple-functions