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 ()
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).
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.
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.
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.
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 ()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With