CS 254 Lab 2: Some Basic Haskell Exercises (aka “Seven Come Eleven”)

This lab should help you get your bearings in the WinHugs environment, learn your way around the text editor and exercise a few basic aspects of Haskell.

As with all labs, you should try to get your work done during lab hours (for most help) or on your own (e.g., at home). When you have the lab finished, you should "demo" it for me (in lab or office hours) so we can discuss your approach, problems you ran into, etc. Make sure I "check you off" as having finished the lab by the end of the demo!

Here are some simple exploratory exercises that you should be able to do based on what you've seen in lecture and the textbook.

(Note: some of these are more "research/exploratory" questions, aimed at getting you to look around the Haskell documentation sites, etc., rather than to specifically write a program.)

For the lab, choose at least seven of the following eleven exercises to demo!

  1. Write a function in Haskell (call it same3) which compares three different values to see if they are all the same. In other words, when applied to the numbers 2, 3 and 4, it should return False, but when applied to 4, (2+2) and (2*2), it should return True.

  2. Write a function poly which evaluates the polynomial given by 3x2 + 5x - 7. In other words, a function which, when applied to a number, will return the value of the formula above for that value of x.

  3. Write a function of two Boolean (Bool) arguments called nor which returns True when neither of its arguments ("neither one nor the other") is True. In all other cases it should return False.

  4. What is the difference between the (infix, arithmetic) operators "**", "^^" and "^" in Haskell?

    (hint: consider typings, but also try a few sample arguments; use the ":t" command to find out the type of an expression, and rememebr to put infix operators in parentheses for this purpose)

  5. What is the definition (i.e., program text) of the "sum" operator in Haskell? How about the "maximum" operator?
    (hint: look in the library file "Prelude.hs": you can do this by having only the Prelude loaded (which is the default at startup) and then typing the ":f <function name>" command)

  6. Using the Hugs reduction count feature (with command ":s +s"), see if you can determine a rule for how many reductions are performed when an integer arithmetic expression with n operators is evaluated. (Express your answer as a function of n.) For example, the expression "2 + 3 * 5" has two operators, whereas "17 * (12 - 4) + 6 / 3" has 4 operators.

    Does the rule change if the literal values are of type Float rather than integers? (Use literals like "4.5" to get Float values.)

  7. The following function twice applies its function argument twice to a value:
    twice f x = f (f x)
    Show how twice can be defined without reference to a second argument (i.e., without the "x" or any other variable in its place).

  8. Consider the following functions defined using twice; for each one, describe what it does in plain English. Also give an example application and result for each.
    nice = twice twice
    slice = twice (twice twice)
    dice = (twice twice) twice

  9. Give alternate, simpler definitions for each of the following (they should be equivalent to the ones given here):
    foo = twice (take 10)
    bar = twice (drop 10)

  10. Write a function stutter which will take a number n and a string s and return a new string which consists of n copies of s joined together. You will want to use the concat function (defined in the prelude) which joins together a list of strings as follows:
    > concat ["this", "is", "a", "test"]
    "thisisatest"

    Your function should behave as follows:
    > stutter 3 "foobar"
    "foobarfoobarfoobar"

    (Hint: you might want to check out the Prelude function called replicate!)

  11. Write another function stammer which will take a number n and a string s and return a new string which consists of all the characters from s, in order, but with each one repeated n times. You may want to use the map function (as defined in the prelude and demo'ed in lecture). Remember also that a string is really just a list of characters.

    Your function should behave as follows:
    > stammer 3 "foobar"
    "fffoooooobbbaaarrr"

    (Hint: you might want to check out the Prelude function called replicate!)