2016-08-13 3 views
2

그래서 난 그냥 번호를 요청하는 프로그램을 만들어 N 및 디스플레이 피보나치 시퀀스의 N 일 기간 :하스켈 콘솔 프로그램 출력은

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$ ./Fib 

4 
Which Fibonacci sequence number do you need? The Fibonacci number you wanted is 3. 
: 내가 컴파일 된 프로그램을 실행할 때

[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. 

는 그러나,이 반환

I.e. 내가 번호를 입력 할 때까지 기다린 다음 한 줄에 모든 프롬프트를 반환합니다. 내가 잘못 했나요? 이 문제를 해결할 방법이 있습니까?

+2

발견 된 답변, http://stackoverflow.com/questions/2500459/wrong-io-actions-order-using-putstr-and-getline (중복으로 표시 할 플래그가 없습니다) –

+0

관련 항목 : https://stackoverflow.com/questions/13190314/haskell-do-monad-io-happens-out-of-order – Gallifreyan

답변

1

stdout은 라인 버퍼링을 사용하는 것처럼 보입니다. 즉, putStr 호출은 putStrLn이 호출 될 때까지 출력되지 않는 버퍼에 출력을 저장하는 것입니다. 프롬프트에 putStrLn을 사용하거나 stdout에서 버퍼링을 비활성화하여 출력을 수정할 수 있습니다.