How to make a Haskell program display a preliminary result in response to user input?

user5284493238

I am writing a program in Haskell which repeatedly takes its most recent result and uses this to compute the next result. I want to be able to see the newest result in response to user input, so I tried something like this:

main = mainhelper 0

mainhelper count = do
 count <- return (count + 1)
 line <- getLine
 if null line
  then do mainhelper count
  else do
   putStrLn $ show count
   return ()

I was hoping that getLine would return an empty line if the user hasn't entered anything, but this doesn't happen, instead the program does nothing until it receives user input. Is there a way around this?

Daniel Wagner

One simple solution is to fork a thread for the complicated computation and communicate with the main UI thread via MVar. For example:

import Control.Exception
import Control.Monad
import Control.Concurrent

thinkReallyHard x = do
    threadDelay 1000000 -- as a proxy for something that's actually difficult
    evaluate (x+1)

main = do
    v <- newMVar 0
    forkIO (forever (modifyMVar_ v thinkReallyHard))
    forever (getLine >> readMVar v >>= print)

You may wonder about the role of evaluate in thinkReallyHard. The subtlety there is that MVars are lazy -- they can contain thunks just as easily as computed values. In particular, this means it's easy to accidentally push all the pure computation from the forked thread into the thread that's reading and using the contents of the MVar. The call to evaluate simply forces the forked thread to finish the pure computation before writing to the MVar.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a powershell program restart on user input

From Dev

How can I make it so the user can input different answers but display the same result?

From Dev

How can I make it so the user can input different answers but display the same result?

From Dev

Ruby - How to create a program that will create a .doc file and display user input?

From Dev

How to loop random number to make result equal user input summary

From Dev

How do I make a program that reverse user input of integers in Java?

From Dev

How do I make a program that reverse user input of integers in Java?

From Dev

Leksah 0.15.0.1, GHC 7.10.1 : Is it possible to pass user input to a Haskell program which is launched inside Leksah Haskell IDE? If yes, how?

From Dev

How to display the user input in a panel

From Dev

How to make a Makefile for a program for Haskell Language?

From Dev

Haskell - Result of a program

From Dev

How to make program display an error message if the input doesn't start with a letter

From Dev

How can i make C program that scans user's input(text) and save it on a dynamic string

From Dev

How can I make a user input 20 be read by the program as 20% or 0.20

From Dev

PHP how to make the value of the result display in textbox

From Dev

PHP how to make the value of the result display in textbox

From Dev

How to make a program that does not display the console window?

From Dev

Display result in same field that had user input [PHP]

From Dev

Subtracting two user input numbers, with a move and display the result

From Dev

I need a program to display user input digit and sum of those digits

From Dev

How to run a local program with user input in Perl

From Dev

How to open a program using user input

From Dev

How to Make it so that the User ends this program?

From Dev

Trying to make a Simple User input to character output Console program

From Dev

How to display input back to the user on an HTML page?

From Dev

How to display user input with a Javascript function

From Dev

How to display the longest line from user input

From Dev

How to display user input in an Alert? (SWIFT)

From Dev

How to make arrow keys and backspace work correctly when asking input from user in C program using termios.h?

Related Related

  1. 1

    How to make a powershell program restart on user input

  2. 2

    How can I make it so the user can input different answers but display the same result?

  3. 3

    How can I make it so the user can input different answers but display the same result?

  4. 4

    Ruby - How to create a program that will create a .doc file and display user input?

  5. 5

    How to loop random number to make result equal user input summary

  6. 6

    How do I make a program that reverse user input of integers in Java?

  7. 7

    How do I make a program that reverse user input of integers in Java?

  8. 8

    Leksah 0.15.0.1, GHC 7.10.1 : Is it possible to pass user input to a Haskell program which is launched inside Leksah Haskell IDE? If yes, how?

  9. 9

    How to display the user input in a panel

  10. 10

    How to make a Makefile for a program for Haskell Language?

  11. 11

    Haskell - Result of a program

  12. 12

    How to make program display an error message if the input doesn't start with a letter

  13. 13

    How can i make C program that scans user's input(text) and save it on a dynamic string

  14. 14

    How can I make a user input 20 be read by the program as 20% or 0.20

  15. 15

    PHP how to make the value of the result display in textbox

  16. 16

    PHP how to make the value of the result display in textbox

  17. 17

    How to make a program that does not display the console window?

  18. 18

    Display result in same field that had user input [PHP]

  19. 19

    Subtracting two user input numbers, with a move and display the result

  20. 20

    I need a program to display user input digit and sum of those digits

  21. 21

    How to run a local program with user input in Perl

  22. 22

    How to open a program using user input

  23. 23

    How to Make it so that the User ends this program?

  24. 24

    Trying to make a Simple User input to character output Console program

  25. 25

    How to display input back to the user on an HTML page?

  26. 26

    How to display user input with a Javascript function

  27. 27

    How to display the longest line from user input

  28. 28

    How to display user input in an Alert? (SWIFT)

  29. 29

    How to make arrow keys and backspace work correctly when asking input from user in C program using termios.h?

HotTag

Archive