The user will enter a number of cases, the length of the case, and then the case. The number of cases varies each time. Example of user input:
2
4
"four"
3
"the"
I need to do something to each of the cases, but how do I get the execute the do loop to match the number of cases?
This is what I have so far:
main = do
numOfCases <- getInteger
caseLength <- getInteger
case <- getLine
putStrLn $ doSomething case
--how do I call the loop exactly once more, but this time without the numOfCases?
Thanks.
Use replicateM_
, which repeats an action a given number of times. You import it from Control.Monad
:
replicateM_ :: (Monad m) => Int -> m a -> m ()
So you would use it like this:
import Control.Monad (replicateM_)
main = do
numOfCases <- readLn
replicateM_ numOfCases $ do
caseLength <- readLn
str <- getLine
... -- do other stuff
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.