Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:11:17+00:00 2026-06-02T01:11:17+00:00

I would like my commandline Haskell program to function like this: program wait for

  • 0

I would like my commandline Haskell program to function like this:
program wait for user input,

  1. user type something, push “enter”
  2. Haskell process the input, shows the result on stdout
  3. Haskell waits for next user input
  4. If no more input, user terminate program by Ctrl+D

I tried getContents. But getContents wait for user to type all lines before processing them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:11:20+00:00Added an answer on June 2, 2026 at 1:11 am

    There’s a lot of confusion going on here. Let’s try to clear things up.

    I tried getContents. But getContents wait for user to type all lines before processing them.

    The most likely thing here is that you’ve compiled your program, and didn’t notice that the default buffering for output was block-buffering. This is easy to fix:

    f line = putStrLn ("Hi, " ++ line ++ "!")
    
    main = do
        hSetBuffering stdout LineBuffering -- or use NoBuffering
        putStrLn "Enter some names."
        input <- getContents
        mapM_ f (lines input)
    

    You should use NoBuffering if you don’t plan on printing a whole line (including newline) after each line of user input.

    For a more precise answer, we’ll need to see the code you tried that didn’t work.

    Q: But in my first try, I use: “interact show” and it doesn’t work. Do you know why?
    A: Because show will not return any output until its entire input has been exhausted.

    This answer is not quite correct. The real answer is that show produces a string with no newlines in it! (Though the character sequence ['\\','\n'] does show up sometimes if the input is more than one line long.) So, for interact show, you really must use NoBuffering on stdout. For example, if you use this:

    main = do
        hSetBuffering stdout NoBuffering
        interact show
    

    …the program will print a bit more output after each line. You might also want to set stdin‘s buffering to NoBuffering (instead of the default LineBuffering), since show is productive enough that it really can produce more output after each keystroke.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to catch all the inputs and output of a commandline program
I would like to achieve this functionality. <p:column> <p:commandLink value=prihlasit oncomplete=dlg.show(); action=#{signForProjectBean.setProjectForDetail(item)} /> </p:column>
I would like to write a small commandline scanner using ClamAV and Python. I
I would like to use redis to query data from commandline, scripts, web and
For unit tests I would like to mimic different commandline arguments. How do I
I would like to have MSTest (honestly, I'd take whatever at this point) run
I would like to create a user called webapp that can access a database
I would like a command line function that I can run on any file
I would like to execute command line application before each commit (push) to central
I'm setting up a build system for my Haskell project and would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.