University of Toronto

Homework 2: Pure and Applied tex2html_wrap_inline120 -Calculus

  1. Convert the following into tex2html_wrap_inline122 -normal form:
    1. tex2html_wrap_inline124
    2. tex2html_wrap_inline126
    3. tex2html_wrap_inline110
    4. tex2html_wrap_inline130
  2. (Recursive functions.) Using the applied tex2html_wrap_inline120 -calculus of Section 14.4 of Sethi, define the following functions:
    1. Term power a b which returns tex2html_wrap_inline134 . You may assume that tex2html_wrap_inline122 . (Hint: use function times.)
    2. Function fib n to compute the n'th element of the Fibonacci sequence. For example, fib 2 = 2, fib 4 = 5.
  3. (Lists.) A list is an ordered sequence of elements. Notation [a b] stands for a list of 2 elements, a and b. In this problem, you are asked to implement a couple of functions for lists using tex2html_wrap_inline120 -calculus. [ ] indicates a list with no elements in it.

    Add the following constants to our tex2html_wrap_inline120 -calculus: cons, first, rest, isnil. first is a term that returns the first element of the list. For example, first [a b] = a. rest returns the list without the first element. For example, rest [a b] = [b]; rest [a] = [ ]. cons takes an element and a list and inserts the element in the beginning of the list. For example, cons a [b] = [a b]. Lists can be nested in each other, i.e. an element of a list can be a list. For example, cons [a] [b c] = [[a] b c]. Here, the first element is [a], a list.

    The following are some of the tex2html_wrap_inline144 -rules for these constants:

    tabular38

    1. Write a recursive function toa which goes through the list of elements, changing each to a. toa [ ] should return [ ]. toa cons M N should change M to a and then recursively go through elements of N, changing each to a (you can do this by cons a N). toa [a b c [d e] ] = [a a a a].
    2. Modify toa to replace all elements of the list (sublists included) by a. To do this, we will need to define one more constant, islist. It returns true if its argument is a list and false otherwise.

      tabular77

      So, islist a = false whereas islist [a] = true. The new function is called toarec. To solve this problem, we will need the following definition: toarec(cons M N) - if M is a list, we have to process M recursively. Else just replace M by a. So, toarec [a b c [d e] ] = [a a a [a a] ].




next up previous
Next: About this document

Marsha Chechik
Thu Sep 26 10:55:50 EDT 1996