haskell while-loop do-while

Haskell-Do while loop



while-loop do-while (2)

En Haskell escribes "bucles" recursivamente, la mayoría de las veces.

import Control.Monad -- .... start = do putStrLn "Before the loop!" -- we define "loop" as a recursive IO action let loop = do putStrLn "Hello, what is your name?" name <- getLine putStrLn $ "Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory." putStrLn "You will receive fifty questions in total to which you can reply with Yes or No." putStrLn "Whenever you feel ready to begin please write Start" goGlenn <- getLine putStrLn goGlenn -- if we did not finish, start another loop when (goGlenn /= "start") loop loop -- start the first iteration putStrLn "After the loop!"

¡Soy nuevo para Haskell y estaría contento si alguien estuviera dispuesto a ayudarme! Estoy intentando que este programa funcione con un ciclo do while.

El resultado del segundo comando getLine se pone en el goGlenn variable y si goGlenn no es igual a "start", el programa volverá al principio

start = do loop $ do lift performAction putStrLn "Hello, what is your name?" name <- getLine putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory.") putStrLn "You will receive fifty questions in total to which you can reply with Yes or No." putStrLn "Whenever you feel ready to begin please write Start" goGlenn <- getLine putStrLn goGlenn while (goGlenn /= "start")


No estoy seguro, tal vez esta versión te puede ayudar a:

import Control.Monad loop action = do condition <- action when condition (loop action) while = return start = let action = do { putStrLn "Hello, what is your name?"; name <- getLine; putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory."); putStrLn "You will receive fifty questions in total to which you can reply with Yes or No."; putStrLn "Whenever you feel ready to begin please write Start"; goGlenn <- getLine; putStrLn goGlenn; while (goGlenn /= "start"); } in loop action

(Editar) o puede ser también:

start = loop (do { putStrLn "Hello, what is your name?"; name <- getLine; putStrLn ("Welcome to our personality test " ++ name ++ ", inspired by the Big Five Theory."); putStrLn "You will receive fifty questions in total to which you can reply with Yes or No."; putStrLn "Whenever you feel ready to begin please write Start"; goGlenn <- getLine; putStrLn goGlenn; while (goGlenn /= "start"); })