Wednesday, February 13, 2013

HASKELL USER INPUT

HASKELL USER INPUT

REVISED: Wednesday, January 24, 2024




Haskell user input.

Imperative programming is monadic; and, monadic do blocks look like imperative code.

I.  HASKELL USER INPUT

A. WRITE HASKELL PROGRAM

Haskell is a lazy language, the order in which operations are evaluated in it is unspecified. do allows us to specify the order of operations. Therefore, if we want to write a function that is interactive one way to do this is to use the do keyword. However, the last statement in a do block must be an expression. For example, as shown below the last statement is a putStrLn which starts an expression.

For example; "Copy Paste" the following Haskell program into your text editor:

module Food
    where

main = do  
    putStrLn "Type your favorite food or type exit and press Enter."  
    food <- getLine  
    if food == "exit"
      then do 
        return ()
      else do  
        putStrLn ("Amazing! My favorite food is also " ++ food ++ "!")
        main

From your text editor do a "File, Save As", and save the file as Food.hs to your working directory.

To get the value out of an I/O action, you have to perform it inside another I/O action by binding it to a name with <-, a function arrow pronounced "drawn from".

Read "food <-  getLine" as, "Perform the I/O action getLine and then bind its result value to food". The getLine function works with <- and binds input to a variable. getLine has a type of IO String, so food will have a type of String. In other words, food is "drawn from" getline.

In a do block, the last action cannot be bound to a name. The value from the last action is extracted and bound by the do block to the result.

B. LOAD HASKELL PROGRAM INTO GHCi INTERPRETER

After the GHCi  Prelude> prompt type :load Food as shown below, then press Enter:

Prelude> :load Food
[1 of 1] Compiling Food             ( Food.hs, interpreted )
Ok, modules loaded: Food.

C. CALL MAIN FUNCTION IN GHCi

Prelude>  main
Type your favorite food or type exit and press Enter. 
Steak   -- Enter your favorite food at the prompt.
Amazing! My favorite food is also Steak!
Type your favorite food or type exit and press Enter.
exit
it :: ()   -- Appears because of options currently set: +t
Prelude>  

II. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.

In this tutorial, you have received an introduction to Haskell user input.