2

We are superduper-beginners in Haskell and are struggling with a school project. Our goal is to create a working memory test. We have finished everything but the last piece of the puzzle. We want the program to show one word for about a second, and then disappear. Then we want this to be repeated 25 times, for all the 25 words we want to include in the test.

We've been trying to use threadDelay and clearLine without success. Any help we could get is much appreciated, is there some sort of magic function in HASKELL we could use?

This is the only thing we have left to solve.

Hope someone can help us.

/Sara

This is the program so far:

import Data.List

main :: IO Int
main =
 do
  putStrLn "Hej och välkommen! Du ska nu få testa ditt arbetsminne, i ett litet test. Du kommer få 25 ord presenterade för dig, ett åt gången. Du ska försöka komma ihåg så många ord som möjligt. När du känner dig redo, skriv ditt namn och tryck Enter." --Translation: Hello, this is a working memory test blablabla when ready print redo
  name <- getLine
  text <- readFile "ord.txt" 
  let ord = words text
  putStrLn "Vilka ord kommer du ihåg? Skriv ner de ord du kommer ihåg, med små bokstäver och mellanslag, utan kommatecken." --Translation: What words do you remember
  s <- getLine
  let svar = words s
  let result1 = map (\x -> elem x ord) svar
  let result = filter (\x -> x == True) result1
  putStrLn (name ++ "! Av 25 ord kom du ihåg:") --Translation: Out of 25 words you remembered...
  return $ length result

We want the part where the words are shown between "name <- getLine" and "text <- readFile "ord.txt" this is what we have tried:

import System.Console.ANSI
import Control.Concurrent

main = do
 putStr "hej"
 threadDelay 3000000 ; clearLine
 putStr "bra jobbat"

We've been trying different locations for threadDelay but it doesn't do what we want it to do.

Sara
  • 59
  • 2

1 Answers1

2

There is a operating system buffer between input and output from programs and the console presented to the user. As explained in the answers to another question, the IO actions like putStr that write to the console only send the data to the buffer; they don't necessarily force the operating system to "flush" the buffer and send the data on to the user. If you pay close attention while the program is running you'll see that nothing is displayed for the first 3 seconds, then it jumps straight to "bra jobbat" without ever displaying "hej". "hej" was sent to the buffer to display before waiting, but it wasn't flushed until later. You can tell the operating system to flush the buffer with hflush from System.IO. You need to flush the buffer for standard output, stdout.

import System.Console.ANSI
import Control.Concurrent
import System.IO

main = do
 putStr "hej"
 hFlush stdout
 threadDelay 3000000 ; clearLine
 putStr "bra jobbat"
 hFlush stdout
Community
  • 1
  • 1
Cirdec
  • 24,019
  • 2
  • 50
  • 100