Wednesday, 27 May 2015

Question and Answer

The next quickie comes from Yet Another Haskell Tutorial by Hal Daume III

We write a string asking for the user's name, and then write back out a reply containing the name:-

module Main

import System.IO

main = do
  hSetBuffering stdin LineBuffering
  putStrLn "Enter your name: "
  name <- getLine
  putStrLn ("Hello " ++ name ++ " have a scone")

But first... we apply the action hSetBuffering to the standard input with the option LineBuffering
so that the input does not wait to fill a block buffer before moving on but returns as soon as it has a line of input to work with.

The next action writes the string

Then we read a line from the terminal and assign name to the return value

Now we can concatenate a new reply together and write it out.

Now, when I did this import line:

import IO

I got this message from the compiler:

    Could not find module `IO'
    It is a member of the hidden package `haskell98-'.
    Use -v to see a list of the files searched for.

But the compiler was happy when I changed this to

import System.IO

It looks from the documentation that the one embraces the other.  Something to look up.

>ghc main.hs -o helloname.exe
[1 of 1] Compiling Main             ( main.hs, main.o )
Linking helloname.exe ...

Enter your name:
Hello Rocky have a scone

No comments:

Post a Comment