<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><!--Converted with LaTeX2HTML 96.1-beta (Jan 15, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds -->CS471 Programming Language Structure I
Fall 2001

Homework 5

1.      The function to get the length of a list is usually not tail recursive:

(define (length lis)
    (if (null? lis)
        0
        (+ 1 (length (cdr lis)))))


Write a function called lengthTR that is tail recursive. Hint, you will need an accumulator.

2.      The usual definition of the Fibonacci sequence is:

F(0) = 0
F(1) = 1
F(n) = F(n-2) + F(n-1)

 

a.       Why is this definition inefficient if it was translated directly into Scheme or ML? (Hint: write out the derivation of the expression for F(6).)

b.      Another definition is given by the sequence of pairs: (0,1), (1,1), (1,2), (2,3), (3,5) … etc. Write a Scheme or ML function that, given a pair in the sequence, returns the next one. (Hint: in ML, a function with two arguments can be thought of a function one argument which is a pair. e.g. fun duple (x,y) = (x,y) is a function that returns its single argument unaltered. In Scheme a duple can be represented by a list of two items.)

c.       Write a function using your function from part b that returns the nth pair (starting from zero) in the Fibonacci sequence. e.g. fib(3) returns the 2-tuple (2,3) and fib(4) returns (3,5).

d.      How does the efficiency of this function compare with your function in part a?

3.      Write a function in Scheme or in ML called filter that, given a list of numbers, will return a list that does not include numbers that pass a test which is one of filter’s parameters. For example, given a function odd? that returns true if its argument is an odd number, then (filter odd? ‘(1 2 3 4)) returns the list (2 4). (In ML this would be filter odd [1,2,3,4].)

 

Grading

This assignment is worth 50 points. Part 1 is worth 10 points, part 2, 25 points and part 3, 15 points..

 

Testing

There is no need to prove your functions by running them, but if you want to, the ML system is called 'sml'. A file containing function definitions may be loaded by  'use "file"'. ML may be exited with control-D. 

The scm Scheme interpreter is also available. You can add this line to your .cshrc to use the interpreter.

setenv PATH ${PATH}:/home/radon1/rth/public/Scheme/scm/

The interpreter is invoked by:

% scm -no-init-file

 

You will get a prompt: > at which you can type Scheme expressions to

be evaluated. Type (quit) to exit the interpreter.

At the moment it only runs on Solaris, so will need to rsh to a Solaris box to use it if you are running on a Linux box. It is a command line system; there is no X support available. Files of Scheme definitions may be loaded with:

(load "<file-name>")

 

as a command to the interpreter.

Submission

This assignment must be submitted through WebCT, so it must be prepared electronically. Read the drop box information page before submitting it. Pay close attention to the availability “window” of the assignment. When the window closes, you will not be able to submit an answer.