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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:33:22+00:00 2026-05-28T13:33:22+00:00

I don’t understand why this code does loop only once then exit ? in

  • 0

I don’t understand why this code does loop only once then exit ?
in Ghci I can answer only to the first loop then it seems the variable cont is set to false and I don’t have the prompt to answer.

The result is:

*Main> testLoop1 td10
test
Do you want to continue? (y/N)
y
we continue
test
Do you want to continue? (y/N)
We stop

code:

type TDeckSTIO    = StateT TableDecks IO

continue = do
putStrLn "Do you want to continue? (y/N)"
c <- getChar
return $ c == 'y'


loop1 :: TDeckSTIO () 
loop1 = do 
    liftIO $ putStrLn "test"
    cont<- liftIO continue 
    if cont
    then do 
        liftIO $ putStrLn "we continue"
        liftIO $ testLoop1 td

    else liftIO $ putStrLn "We stop"

testLoop1 td =  runStateT (loop1 ) td   >> return ()
  • 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-28T13:33:24+00:00Added an answer on May 28, 2026 at 1:33 pm

    The problem is that when you type y and hit enter, that’s actually typing two characters: 'y' itself, and the newline character that gets sent by pressing the return key. The first time round, the loop sees the 'y', but the next time round, it sees the '\n', and since '\n' isn’t 'y', it exits.

    You can either do hSetBuffering stdin NoBuffering before you enter your loop (you’ll need to import System.IO), which will let you process characters without waiting for a newline, or specifically process lines at a time:

    continue = do
      putStrLn "Do you want to continue? (y/N)"
      s <- getLine
      return $ s == "y"
    

    By the way, instead of writing liftIO $ testLoop1 td, you can just stay in the same state monad: you can replace it with loop1 and it’ll work exactly the same.

    Also, testLoop1 is better written as:

    testLoop1 = evalStateT loop1
    

    evalStateT is like runStateT, but doesn’t include the final state, so you don’t have to explicitly discard the value with >> return ().

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

Sidebar

Related Questions

Don't let below code scare you away . The question is really simple, only
I don't understand where the extra bits are coming from in this article about
Don't know if anyone can help me with this or if it's even possible.
Don't know a whole lot about streams. Why does the first version work using
Don't be to hard on me, this is my first try with JSF. I
Don't these two mean the same thing, first get the value and then increment?
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't ask me why but i can't use this method because I need to
Don't know why I can't reply to people on here with only a small
Don't know why but I can't find a solution to this. I have 3

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.