Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop with StateT: Why this loop doesn't loop

Tags:

haskell

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 ()
like image 470
jinkou2 jinkou2 Avatar asked Jan 23 '12 15:01

jinkou2 jinkou2


People also ask

Why while loop is not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

Why is my for loop running infinitely?

An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0.

How do you stop a repeating loop?

A repeat loop is used to iterate over a block of code multiple number of times. There is no condition check in repeat loop to exit the loop. The only way to exit a repeat loop is to call break.

Why does my while loop not stop?

The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop. Inside of your for loop, you have the code looping from 1-10.


1 Answers

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

like image 92
ehird Avatar answered Oct 17 '22 21:10

ehird