So I just made a program that asks for a number n and displays n th term of the Fibonacci sequence:
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)
When I run the program in GHCi or through runhaskell, it executes fine; that is, asks me for a number, allows me to enter it on the same line and returns a number on another line:
Gallifreian@Gallifrey:~/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.
However, when I run the compiled program, it returns this:
Gallifreian@Gallifrey:~/Haskell$ ./Fib
4
Which Fibonacci sequence number do you need? The Fibonacci number you wanted is 3.
I. e. waits for me to enter a number and then returns all prompts on one line. What have I done wrong? Is there a way around this?