(lwords,words,x:xs)<-get
x:xs
matches a list with at least one element (x
becomes the first element, and xs
becomes the rest of the list), so you get a pattern match failure when the third member of the tuple is []
.
Solution: replace
(lwords,words,x:xs)<-get
case x:xs of
with
(lwords,words,xs)<-get
case xs of
(And consider using different variable names later in the function: it gets confusing when you have two or more variables with the same name.)
Edit: if you turn on warnings (pass the -Wall
flag to ghc/ghci), you will get warned at compile time that the pattern may fail to match at runtime. (You'll also get a warning about having one xs
variable hiding another xs
variable, called shadowing.)