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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:13:38+00:00 2026-06-13T22:13:38+00:00

Because I oversimplified in my other question before, I would like to give a

  • 0

Because I oversimplified in my other question before, I would like to give a more clear example here.

How can I handle situations where I have to check for certian conditions in a sequential way without nesting multiple cases? With “sequential way” I mean getting a value (e.g. from stdin), checking this value for a certain condition and depending on the outcome getting another value and so on.

Example:

sequen :: IO String
sequen = do
  a <- getLine
  case a of
    "hi" -> do
      putStrLn "hello!"
      b <- getLine
      case b of
        "how are you?" -> do
          putStrLn "fine, thanks"
          return "nice conversation"
        _ -> return "error 2"
    _ -> return "error 1"

I know that there are better ways to write such a chat bot, it should just demonstrate the sequential nature of the problem. As you can see, with every nested case, the code also gets indented deeper.

Is there a way to better structure such code? I’m thinking of handling the “errors” on one place and describing the “success-path” without the error handling distributed all over it.

  • 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-13T22:13:39+00:00Added an answer on June 13, 2026 at 10:13 pm

    Of course. This is precisely what EitherT was made for. You can get it from Control.Monad.Trans.Either in the eitherT package.

    import Control.Monad.Trans.Class
    import Control.Monad.Trans.Either
    
    main = do
        e <- runEitherT $ do
            a <- lift getLine
            case a of
                "hi" -> lift $ putStrLn "hello!"
                _    -> left 1
            b <- lift getLine
            case b of
                "how are you?" -> lift $ putStrLn "fine, thanks!"
                _              -> left 2
            return "nice conversation"
        case e of
            Left  n   -> putStrLn $ "Error - Code: " ++ show n
            Right str -> putStrLn $ "Success - String: " ++ str
    

    EitherT aborts the current code block whenever it encounters a left statement, and people typically use this to indicate error conditions.

    The inner block’s type is EitherT Int IO String. When you runEitherT it, you get IO (Either Int String). The Left type corresponds to the case where it failed with a left and the Right value means it successfully reached the end of the block.

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

Sidebar

Related Questions

Because shells other than ksh do not support pass-by-reference, how can multiple arrays be
Because apparently everyone hates sub selects, I would like to do this using joins.
Because I would like to make some tests with the libpcap and a small
Because data from file look like this: line 1 is name (first last), next
Because anyone can sign up and then log in,... and because a user isn't
Because of an unfortunate Windows limitation (no more than 16 icon overlays in Windows
Because if main() creates an NSAutoreleasePool , and drains it before the program exits,
Because of all the problems we can meet when trying to use Hibernate in
I've oversimplified this a bit, because I'm looking for a general-purpose answer. Let's say
Because I want to try out another extension I would have two extensions which

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.