CS 154 Lab 1: Some Basic Haskell Exercises

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. These are meant as exploratory exercises that you should be able to do based on what you've seen in lecture and the textbook. The first few should be quite easy, while some of the later ones might require a bit of a “stretch”, but just ask if you need help.

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!

  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 5x2 + 3x - 2. 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. 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"]

    Your function should behave as follows:

    > stutter 3 "foobar"

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

  5. 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"

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

  6. 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. Specifically, write into a file and be able to demonstrate an alternate version of the function twice (if you want to define both functions in the same file for the sake of comparison, you can call one of them twice', with a trailing single quote.)

    (Hint: you might want to check out the Prelude operator called (.).)

  7. 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; i.e., show an example of it being used to compute something concrete.
    nice = twice twice

    slice = twice (twice twice)

    dice = (twice twice) twice

  8. 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)