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 problem is that when you type
yand 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 NoBufferingbefore you enter your loop (you’ll need to importSystem.IO), which will let you process characters without waiting for a newline, or specifically process lines at a time:By the way, instead of writing
liftIO $ testLoop1 td, you can just stay in the same state monad: you can replace it withloop1and it’ll work exactly the same.Also,
testLoop1is better written as:evalStateTis likerunStateT, but doesn’t include the final state, so you don’t have to explicitly discard the value with>> return ().