gpt4 book ai didi

Haskell - 执行 while 循环

转载 作者:行者123 更新时间:2023-12-02 14:16:20 27 4
gpt4 key购买 nike

我是 Haskell 新手,如果有人愿意帮助我,我会很高兴!我试图让这个程序与 do while 循环一起工作。

第二个 getLine 命令的结果被放入变量 goGlenn 中,如果 goGlenn 不等于“start”,则程序将返回到开头

    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")

最佳答案

在 Haskell 中,大多数时候你会递归地编写“循环”。

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!"

关于Haskell - 执行 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com