2012-12-04 60 views
3

我想在Haskell中編寫一個函數,它檢查一些事情,然後基於一些最小用戶輸入遞歸。爲了做到這一點,我認爲我必須使用do塊。在Haskell中嵌套````塊

cip :: [Argument] -> [Argument] -> Bool -> Bool -> IO() 
cip (a:args) pargs burden gameover = do 
    let nasko = a:pargs 
    putStrLn (getPremise a) 
    let newgraph = Carneades.mkArgGraph nasko 
    let newcaes = (CAES (newgraph,audience2,assStandarts)) 
    let answer = (acceptable (mkProp (getPremise a)) newcaes) 
    print answer 
    if(answer==True) 
    then (cip args nasko burden gameover) 
    else do 
     print "One of the arguments is not proved. Here are the premises that need proving" 
     print (propsForFixing newcaes a) 
     print "Let's see what you have for the first Propositon" 
     --add an if to check if no applicable arguments. 
     print (argumentScanHelp (head (propsForFixing newcaes a)) args) 
     print "\n Would you like me to apply the firt one? Y/N" 
     choice <- getLine 
     if(choice=="Y") then do print "applying the argument" 
           let applicabee = head (argumentScanHelp (head (propsForFixing newcaes a)) args) 
           print "Argument targeted" 
           let newargs = delete applicabee args 
           let newpargs = applicabee:nasko 
           print "Argument applied sucsessfuly. Recusing again" 
           (cip newargs newpargs burden gameover) 
return() 

它傷害了我的眼睛只是看着它,但這是do塊給你。 一切高達第三do塊是好的。但隨後在這條線:

 if(choice=="Y") then do print "applying the argument" 
           let applicabee = head (argumentScanHelp (head (propsForFixing newcaes a)) args) 

的編譯器開始哭鬧:

Main.hs:209:73: parse error on input `let' 

嘗試了各種不同的壓痕,但我似乎無法得到它的工作。 我不想使用單獨的函數,因爲這意味着我必須經常傳遞很多爭論。

任何人都可以幫助我解決問題嗎?另外一個解釋什麼是嵌套的do塊的規格將不勝感激。

+2

'if(answer == True)'與'if answer'相同 – amindfv

回答

8

錯誤的原因我認爲是錯誤的if 表達式。您使用它,就好像它是如果陳述,存在於大多數命令式語言中。簡單地說,總是有一個else

但是,在塊中,「沒有別的東西」是有意義的,就像if語句沒有別的東西。幸運的是,Control.Monad模塊將爲您提供一個功能正是:

import Control.Monad (when) 

(...) 

when (choice=="Y") $ do print "applying the argument" 
         let applicabee = ... 

你似乎已經使用,這是很好的正確方法,這基本上是你必須正確地縮進嵌套做塊。

PS。另外,請確保您的最後return()像您的其他代碼一樣縮進! DS。

+1

看上去很好。你需要圍繞'do'(或者相當於'when(choice ==「Y」)$ do')的圓括號,否則很好的答案。 –

+0

修正,謝謝Daniel! – Tarrasch