Haskell: Non-exhaustive patterns in function (simple functions) -
i confused why 1st , 3rd versions of functions give error whereas sec definition works fine.
-- head , tail 3rd :: [a] -> 3rd [a] = head (tail (tail[a])) -- pattern matching third2 :: [a] -> third2 (_:_:x:_) = x -- list indexing third3 :: [a] -> third3 [a] = [a]!!2
thanks in advance
that odd sec 1 not complain non-exhaustive patterns, since third2
not match lists of length zero, one, or two. third
, third3
functions complain because [a]
not variable, pattern. [a]
desugars (a:[])
, have written them as
third (a:[]) = head (tail (a:[])) third3 (a:[]) = (a:[]) !! 2
neither of work, single element lists. suspect want is
third = head (tail a) third3 = !! 2
haskell non-exhaustive-patterns
No comments:
Post a Comment