2011-06-08 32 views
0

我有一個問題,這個代碼不工作,我不知道爲什麼:如何在haskell中做出簡單的選擇?

foo :: [String] -> IO [String] 
foo input = do 
    choice <- getLine 
    if choice == "1" then do 
     putStrLn "good choice" 
     return input 
    else 
     return [] 
+3

你是什麼意思「此代碼不工作」..任何編譯器錯誤?運行時錯誤? – Ankur 2011-06-08 10:47:28

回答

2

由於是(有位retabbing)的代碼爲我工作。 if/else很難在標籤中正確顯示。 Wiki文章if/then/else應該有所幫助。

foo :: [String] -> IO [String] 
foo input = do 
    choice <- getLine 
    if choice == "1" then do 
    putStrLn "good choice" 
    return input 
    else 
    return [] 

消除內do表達使得如果/則塊稍微更容易縮進。

foo2 input = do 
    choice <- getLine 
    if (choice == "1") 
    then (putStrLn "good choice" >> return input) 
    else (return [])