This is the first midterm exam that was given to CS1321X students in
Fall 2002. This should give you an idea of the kinds of questions we
might be asking on Thursday. For a true testing experience, turn off
the TV, close the door, and give yourself 80 minutes to answer these
questions and see how you do.
1. (10 points) Below is one possible definition of reverse using append
and list:
(define (reverse rlist)
(cond [(null? rlist) ()]
[else (append (reverse (cdr rlist))
(list (car rlist)))]))
Using the substitution model of evaluation described in class, illustrate
the shape of the process generated by evaluating the expression
(reverse '(a b c)). Is this a linear iterative process or a linear
recursive process?
2. (10 points) Represent the following lists using box-and-pointer notation:
(a ((b) c))
((a (b)) c)
3. (20 points) The function repeat-yourself takes one-argument, a list,
as input, and returns a list described by the following examples:
> (repeat-yourself '(1 2 3))
(1 2 3 1 2 3)
> (repeat-yourself '(is a sentence fragment))
(is a sentence fragment is a sentence fragment)
> (repeat-yourself '(()))
(() ())
In the space below, construct your own version of repeat-yourself using
Scheme. You may use only DEFINE, COND, IF, CAR, CDR, CONS, LIST, APPEND,
EQUAL?, =, >, <, >=, <=, NULL?, LIST?, PAIR?, +, -, *, /, AND, OR, NOT.
You may not need all of these, but they are all available to you.
4. (20 points) The function merge takes two arguments as input. Each
argument is a list, and each list consists of a series of integers sorted
by increasing value. This function returns a single list obtained by
combining the two sorted lists into one sorted list. Here are some examples:
> (merge '(1 2 4 8) '(3 5 6))
(1 2 3 4 5 6 8)
> (merge () '(3 5 6))
(3 5 6)
> (merge '(1 2 4 8) ())
(1 2 4 8)
> (merge '(1 2) '(2 3))
(1 2 2 3)
In the space below, use augmenting recursion to construct your own version of
merge in Scheme. You may use only DEFINE, COND, IF, CAR, CDR, CONS, LIST,
APPEND, EQUAL?, =, >, <, >=, <=, NULL?, LIST?, PAIR?, +, -, *, /, AND, OR,
NOT. You may not need all of these, but they are all available to you.
All functions that you write as part of this solution must use augmenting
recursion.
5. (20 points) Now construct another version of merge, this time using
tail recursion (in Scheme, of course). You may use only DEFINE, COND, IF,
CAR, CDR, CONS, LIST, APPEND, EQUAL?, =, >, <, >=, <=, NULL?, LIST?, PAIR?,
+, -, *, /, AND, OR, NOT. You may not need all of these, but they are all
available to you. All functions that you write as part of this solution
must use tail recursion.
6. (10 points) Explain why the following code doesn't mimic Scheme's
predefined quote function:
(define (my-quote some-value)
(quote some-value))
7. (10 points) The first n terms of the harmonic series are defined as
1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/n
We can rewrite this definition recursively, so that the first n terms of
the harmonic series can be defined as:
harmonic (n) = 1 if n = 1
= 1/n + harmonic (n - 1) if n > 1
Using Scheme and the information provided above, construct the function
harmonic which takes one argument, an integer n which is greater than 0,
and returns the sum of the first n terms of the harmonic series as a real
number. You may use only DEFINE, COND, IF, CAR, CDR, CONS, LIST, APPEND,
EQUAL?, =, >, <, >=, <=, NULL?, LIST?, PAIR?, +, -, *, /, AND, OR, NOT.
You may not need all of these, but they are all available to you.
Copyright (c) 2003 by Kurt Eiselt. All rights reserved, with
the exception of stuff that belongs to somebody else.
Last revised: September 15, 2003