Review for Quiz 4

Question 4-1

Using the Church numerals, we can define the inc function in the lambda calculus to take a Church numeral representing some number n and return the Church numeral representing n+1.

inc = λasz.a s (s z)
Give a lambda calculus definition of the addition function +, which takes two Church numerals representing two numbers m and n and returns a Church numeral representing their sum, m+n.

Question 4-2

Define class as it relates to the Haskell programming language.

Question 4-3

Describe what shows function does - that is, what parameters does the function take, and what does it return?

Solutions

Solution 4-1

λmn.n inc m

Solution 4-2

A class is a set of functions that any type that is a member (instance) of the class must define. For example, we might define a Measurable class:
class Measurable a where
    size :: a -> Integer
Now, if we have a type that we want to include in this class, we can declare it as an instance. The following declares lists of any type of data to be in the Measurable class.
instance Measurable [a] where
    size [] = 0
    size (x:xs) = 1 + size xs

Solution 4-3

The shows function takes two parameters - an item data to convert into a string, and a string suffix - and it returns a string containing a character representation of data followed by the characters of suffix.