Search code examples
haskellfunctional-programming

How to print and read at in the same line in Haskell?


Im comming from C++.

This code:

 int main() {
     int age; 
     std::cout << "Type your age: ";
     std::cin  >> age;
     return 0;
 }

Would produce something like this on terminal:

 Type your age: _

I can enter the age value on the same line of the message.

How can I achieve the same result in Haskell?


Solution

  • You can use putStr function to print message without newline at the end as:

    import System.IO
    
    main = do hSetBuffering stdout NoBuffering
              putStr "Type your age: "
              age <- readLn::IO Int
              return ()