Along with trying the examples from the slides, which is a standing recommendation, here are some exercises to try before Monday. There's nothing to turn in or be graded but you'll learn more if you take the time to give these a try. Feel free to discuss these problems on Piazza, with each other, and/or with me and Dennis.
  1. If you use :type to see the type of each of the following functions, you'll see that some type variables are a's, b's, etc. and some are t, t1, t2, etc. See if you can discern a pattern in which of the two sequences the type variable is chosen from.
    let f1 x = x
    let f2 x = 10
    let f3 x y z = y
    let f4 x y z = x ^ y + 0
    
  2. Create a file named ex2.hs and put these lines in it:
    add x y = x + y
    
    addInt::Int -> Int -> Int
    addInt x y = x + y
    
    i = 10
    j = 10
    
    k = addInt i i
    
    f1 = add 10
    f2 = addInt 10
    
    Load the file using :load and then do :browse to see the types of the bindings. Try the following expressions, :types, and lets, some of which produce errors. Try to predict the result of each. If an error, see if you can understand its cause.
    i
    j
    k
    add i i
    add j j
    addInt i i
    addInt j j
    let i2 = 5
    :type i2
    let i3 = 5::Int
    :type i3
    addInt i2 i2
    addInt i3 i3
    
    Add min3 and eq3 from slide 80 to ex2.hs and then use :r to reload it. Check it with :browse.
  3. Add the definition for sign from slide 81 to ex2.hs. Reload to be sure it's valid, then try adjusting the indentation of the guards, and see if you can figure out what's allowed and what's not.
  4. Bind some operators to identifiers and experiment with some partial applications. Example:
    let m=(*)
    let a=(+)
    let n=negate
    let times10 = m 10
    let plus3 = a 3
    plus3 (times10 (n 5)
    
    Note that I bound the name n to the value of negate, so both n and negate reference the same function.
  5. Enter the following sequence of lets and try to predict the type of each binding.
    let f x y z = y
    let f1 = f 'x'
    let f2 = f 1 2
    let f3 = f 1 f  -- Goes beyond what we've talked about!!
    
  6. Using guards, write a function tempTo units temperature to convert a temperature to either Celsius or Fahrenheit. It works like this:
    > tempTo 'c' 32
    0.0
    > tempTo 'f' 0
    32.0
    > toC 212
    100.0
    > toF 0
    32.0
    
    What are toC and toF doing? How did I create them?
  7. Pretend you've never heard of Gauss's summation formulas and write recursive functions sumN and sumMtoN to compute the sum of the first N natural numbers and the natural numbers from M to N, respectively.
  8. Look through some code you've written and see if you can find some cases where the ability to use a partial application might have saved some repetition. If you've got a good one, post it!
  9. I bet that everybody's used a partial application in a high school math class. Think about it but no spoilers here!
The first ten good follow-ups of any sort to this post will get a point of extra credit on assignment 1. What's "good"? I'll know when I see it, and I bet you will, too! :) (Limit: Probably one per student.) If you find any bugs/typos, mail to me rather than posting. I'll stealthily correct them.