Haskell - 做while循环

Haskell - 做while循环

问题描述:

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

从第二函数getline命令,结果被放入varible goGlenn如果goGlenn不等于“开始”,然后该程序将返回到开始

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

要添加到@志的回答,代码你写的或多或少都是正确的Haskell语法,但是'loop'和'while'这样的东西没有内建.Haskell实际上并没有do-while循环,它有你可以用来实现的递归do-while循环。 Haskell实际上并没有循环,只是递归,你必须学习如何从你使用的命令式语言中重新实现功能。 – bheklilr

+0

@bheklilr尽管我猜gallais在他们的评论中是正确的:上面的代码似乎是从Control.Monad.LoopWhile文档改编的。 – chi

在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!" 
+2

我猜OP正在尝试使用['loop-while'](http://hackage.haskell.org/package/loop-while-1.0.0/docs/Control-Monad-LoopWhile.html)(参见与代码摘录的相似性)。看到进口清单将有所帮助。 – gallais

+0

@gallais有趣的是,我并不知道......不过,我想知道OP是否真的想使用该库,或者只是想达到相同的效果。对于初学者,我建议先学习基本方法。 – chi

+1

而不是写'let loop = ...;循环',我喜欢写'修复$ \ loop - > ...'。当然这纯粹是文体。 – user2407038

不知道,也许这个版本可以帮助你:

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 

(编辑),也可以是太:

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"); 
    })   
+0

如果我们定义一个自定义的辅助函数'loop',我会被强迫内联'action'的定义并直接写'loop $ do ...'。 – chi

+0

但没有问题...(我认为是)请检查编辑答案 –

+0

是的,两种方式都是正确的。第二个更好,至少对我来说。 – chi