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 7429767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:01:37+00:00 2026-05-29T09:01:37+00:00

I’m trying to solve the balanced brackets problem. I don’t want to do continuous

  • 0

I’m trying to solve the balanced brackets problem. I don’t want to do continuous IO, and would rather make a single call to getLine and parse the resulting string. Therefore the function that solves the problem will deal with two different states: The unconsumed part of the input string, and the bracket stack.

I want to set up some functions for manipulating a stack:

type Stack = String

pop :: Stack -> (Char,Stack)
pop (x:xs) = (x,xs)

push :: Char -> Stack -> ((),Stack)
push a xs = ((),a:xs)

That’s all good if I’m operating in the state monad, however I’m operating in the StateT monad

balanced :: StateT Stack (State String) Bool

I know I’ve been told not to have duplicate monads in the stack. I’m doing it this way because I like how it simplifies the push and pop definitions.

Two problems:

  1. No matter what I do I can’t find a way to apply push and pop to the
    Stack contained in the StateT.
  2. I have no idea how to call this from the main function

Here’s the rest of the code

next :: String -> (Maybe Char,String)
next ""     = (Nothing,[])
next (x:xs) = (Just x,xs)

balanced = do
            c <- lift (state next)
            case c of
              Nothing -> return True
              Just c  -> if elem c open 
                         then (push c) >> balanced
                         else if elem c close 
                              then pop >>= \x ->
                                if eq x c
                                then balanced
                                else return False
                              else balanced
          where open  = "<{(["
                close = "])}>"
                eq '(' ')' = True
                eq '{' '}' = True
                eq '<' '>' = True
                eq '[' ']' = True
                eq  _   _  = False
  • 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-05-29T09:01:38+00:00Added an answer on May 29, 2026 at 9:01 am

    Your problem is that your push and pop are just ordinary, non-monadic function which you are trying to use in the monadic do-block. You are using next correctly, since you call it using the state function, but as you probably noticed, state only works for the plain State monad and not StateT.

    We can implement a monad transformer version of state like this:

    stateT :: Monad m => (s -> (a, s)) -> StateT s m a
    stateT f = do
        (x, s') <- gets f
        put s'
        return x
    

    And then use it in the balanced function with push and pop.

    balanced :: StateT Stack (State String) Bool
    balanced = do
                c <- lift (state next)
                case c of
                  Nothing -> return True
                  Just c  -> if elem c open
                             then (stateT $ push c) >> balanced
                             else if elem c close
                                  then stateT pop >>= \x ->
                                    if eq x c
                                        then balanced
                                        else return False
                                  else balanced
              where open  = "<{(["
                    close = "])}>"
                    eq '(' ')' = True
                    eq '{' '}' = True
                    eq '<' '>' = True
                    eq '[' ']' = True
                    eq  _   _  = False
    

    The function is called like this:

    evalState (evalStateT balanced []) s
    

    Where s is the initial string and [] is the initial stack.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.