2
所以,我只是做了一個程序,詢問了一些ñ和顯示器ñ斐波那契序列的日期限:哈斯克爾控制檯程序輸出
import Control.Monad (forever)
main = forever $ do
putStrLn ""
putStr "Which Fibonacci sequence number do you need? "
number <- readLn :: IO Int
putStrLn $ "The Fibonacci number you wanted is " ++ (show $ fib number) ++ "."
putStrLn ""
where fib :: Int -> Integer
fib number = fibs !! number
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
當我運行在GHCI程序或通過runhaskell,它執行得很好;那就是,找我要了許多,讓我進入它在同一行和另一行返回一個數字:
[email protected]:~/Haskell$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
Prelude> :l Fib.hs
[1 of 1] Compiling Main (Fib.hs, interpreted)
Ok, modules loaded: Main.
*Main> main
Which Fibonacci sequence number do you need? 4
The Fibonacci number you wanted is 3.
然而,當我運行編譯的程序,它返回:
[email protected]:~/Haskell$ ./Fib
4
Which Fibonacci sequence number do you need? The Fibonacci number you wanted is 3.
一,即等待我輸入一個數字,然後在一行中返回所有提示。 我做錯了什麼?有沒有解決的辦法?
找到答案,請參閱http://stackoverflow.com/questions/2500459/wrong-io-actions-order-using-putstr-and-getline(我沒有任何標記留下來標記爲重複) –
相關:https://stackoverflow.com/questions/13190314/haskell-do-monad-io-happens-out-of-order – Gallifreyan