Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9303859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:34:56+00:00 2026-06-18T23:34:56+00:00

I am trying to implement Standard words function of Haskell. I am using State

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:34:57+00:00Added an answer on June 18, 2026 at 11:34 pm
           (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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to implement a standard drag and drop image in wpf using
I'm trying to implement this web api autocomplete in a standard mvc app. http://techbrij.com/987/jquery-ui-autocomplete-asp-net-web-api
I'm currently trying to implement a editable details view using a grouped UITableView. I'd
The standard library open function works both as a function: f = open('file.txt') print(type(f))
I am trying to implement the sample bubble chart into my code as a
I'm trying to implement the fixed footer on my site. My problem is, when
I'm trying to implement exception handling for Optimistic lock type exceptions that are thrown
Hello im trying to implement a websocket communication between client and server. the problem
I'm trying to implement what seems to be the standard UI replacement for comboboxes
I was trying to implement a smart pointer class similar to the standard library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.