2016-02-25 129 views
3

我是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

回答

8

在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

3

不知道,也許這個版本可以幫助你:

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