0

I am trying to implement Standard words function of Haskell. I am using State Monad to solve the problem.

My Code is :

type WorS = ([String],String,String)

words' :: State WorS [String]
words' = do
           (lwords, words, x:xs) <- get
           case x:xs of
           (' ':xs) -> (put (words:lwords, [], xs) >> words')
           ([]) -> return lwords
           (_:xs)-> (put (lwords, words ++ [x], xs) >> words')

run_word' ::  String ->[String]
run_word' x = reverse $ fst (runState words' ([], [], x))

When I do:

run_word' "guns and roses"

I get this error:

Exception: Pattern match failure in do expression

The code is loading in ghci without any error. What am I doing wrong?

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Deepali Semwal
  • 139
  • 1
  • 2
  • 9
  • possible duplicate of [Non-exhaustive patterns in function](http://stackoverflow.com/questions/8435575/non-exhaustive-patterns-in-function) – Matt Fenwick Feb 15 '13 at 20:54

1 Answers1

4
       (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.)

dave4420
  • 46,404
  • 6
  • 118
  • 152
  • I did not get what is wrong in the code. Can you please elaborate. – Deepali Semwal Feb 15 '13 at 19:44
  • 1
    Every time you iterate through the loop, the list in the third element of the state gets shorter. When it becomes the empty list, you get the pattern match failure. – dave4420 Feb 15 '13 at 19:46