Thursday, August 16, 2012

Haskell 1.2.4

Basic IO Exercises


    Before we start on the exercises, I'll quickly recap the workflow concerning the two applications we use to write Haskell modules: the text-editor and ghci. All of the exercises below should be written-out in your text-editor first. Use the comment operator '--' at the beginning of lines containing notes to yourself on what each part of the program does. Ghci will automatically skip over commented notes. Make sure to start your code in the upper-leftmost corner of the text-editor to eliminate the possibility of weird compile-time errors. When you are ready to save the file, save it within your 'haskellPrograms' folder under a name that has no spaces, punctuation-marks, or other uncommon characters. For the interest of consistency, I urge you to use the Haskell naming-convention; something like this: 'exerciseChp1.hs'. Remember that '.hs' is the required suffix, and 'haskellPrograms' is the required folder. 
    Now navigate to 'haskellPrograms' within your terminal application. Once there, type 'ghci' in the terminal. Now you are ready to load your programs! Remember, to load a program you type ':l programName' into ghci.

Exercise 1.1

    This is a basic exercise to help you get a feel for how to use 'putStrLn'. Create a short IO script using the 10 IO Rules of Haskell which apply. First, create a script that takes a number from the user, multiplies it by two, and prints the results onscreen. The idea with this first script is to collect a number from the user into a variable, use that variable as an argument in a separate arithmetic function, pipe the output of the arithmetic function back into a variable within main, and finish by printing the last variable along with some message like 'Two times the number you entered is:' followed by the correct answer. The main tools you need will be:

  • *
  • <-
  • let
  • putStrLn 
  • ++
  • Parens and Double-Quotes 

    You will need to use every rule here except rule #8. If you don't remember, go back and carefully read the special rules for using 'let','read', '++',  and 'putStrLn' which are listed and highlighted in Chapter 1.2.

Exercise 1.2

    Now modify the successful script you created for the last exercise. Try using the arithmetic function as an argument for putStrLn. Now read the compiler-errors. Can you see how the compiler-error messages reflect which specific rule have been broken? Modify the arithmetic function so that it uses 'read' in the argument. Check the error-messages for clues to what is happening. Although the error-messages are difficult to understand, they do give you hints when things go wrong.
    
Exercise 1.3
    
    In this exercise, you must create a simple program which asks for the user's first and last name separately, then displays a message welcoming them to Haskell by their full-name. You must then load the program module (module for short) into ghci and run it. Shown below is the logical breakdown of each step within the program. For starters, it's always good to create a vision of how you think the program should ideally function, despite the fact that you may not know how to accomplish it.  
    Here's how I imagine the program to work. Notice the term 'main>'. This is the ghci 'prompt' which (as you might expect) prompts the user to action. As such, it is not part of our program's design. Likewise, the red-colored text represents data typed by the user, and is also not part of the code we expect to write.

main>Please enter your first name below:
Ryan
main>Now enter your last name below:
Johnson
main>Haskell officially welcomes Ryan Johnson to the world of code!

    To accomplish this simple program you will need to do four things:
  1. Create a 'main=do' function since this is an IO program.
  2. Create two separate functions which display a unique message and retrieve the first and last names of the user.
  3. Combine the first and last name into one unit, as shown above, which features correct spacing*.
  4. Display a greeting which 'pastes' the first and last name of the user into the middle of a pre-written phrase, thus creating a customized greeting message.
    To do this you will need these operators, covered in section 1.2:
  • putStrLn
  • getLine
  • <-
  • ++
  • Parens and Double-Quotes
    You must carefully apply these rules of Haskell IO:
  • Rule #1
  • Rule #2
  • Rule #3
  • Rule #4
  • Rule #6
  • Rule #7
    Ok, now start writing and trying out your creations. You will know you have succeeded when you get the same results as those listed in our creative vision above.

*HINT: Did you know that a space is also a Character in Haskell? You can even make a String of spaces using double-quotations like this: "          ". 








No comments:

Post a Comment

Note: Only a member of this blog may post a comment.