In this first machine problem you will be implementing a number of different Racket functions that make use of recursion (tail and nontail), manipulate lists, and perform pattern matching.
All the code you write for this machine problem will go into the "mp1.rkt" file, found in the private source repository created when you accepted my GitHub invitation. In that file you will find stubs for each of the programming exercises described below.
Note that you may also refer to the assignment writeup found in your repository.
Each exercise below requires you to implement a single function. Unless explicitly mentioned, you should not use any builtin functions or special forms not covered in the first three lectures (01intro, 02functions, 03recursion). If you wish, however, you may implement additional helper functions on top of the required ones.
Note that some exercises specify whether your solution should be tailrecursive (or not). If unspecified, you may do either.
Each exercise is worth 5 points, for a total of 40 possible points.
iexpt
: Performs integer exponentiation (assuming positive, integer
arguments). Given arguments n
and e
, computes n
^{e}. E.g.,
> (iexpt 2 10)
1024
> (iexpt 5 3)
125
polyeval
: Given coeffs
and x
, where coeffs
is a list of integer
coefficients (a, b, c, ...) and x
is an integer, computes the value
of the polynomial ax^{0} + bx^{1} + cx^{2} + ...
E.g.,
> (polyeval '(2 3 4) 2)
24
> (polyeval '() 10)
0
> (polyeval '(8 4 2 1) 4)
120
Your implementation should be tailrecursive. You may use iexpt
from the
previous exercise.
concatenate
: Given zero or more list arguments, concatenates all the
elements of the argument lists into a single list. E.g.,
> (concatenate '(1 2 3) '(hi bye) '(4 5 6))
'(1 2 3 hi bye 4 5 6)
> (concatenate '(a (b) c) '() '((d) (42 43)))
'(a (b) c (d) (42 43))
Your implementation should be tailrecursive. Additionally, when building
your result list, you should only use cons
, and your implementation should
have linear runtime w.r.t. the total number of elements. Hint: it might be
helpful to use reverse
at some point in your implementation!
merge
: Takes two argument lists, both of which contain only integers and
are in ascending order. Returns a single list containing all the integers,
sorted in ascending order. E.g.,
> (merge '(1 4 8 10) '(2 3 7 13))
'(1 2 3 4 7 8 10 13)
Your implementation should not be tailrecursive (we'll do that next).
mergetail
: reimplement the previous function, but this time your
implementation should be tailrecursive. As with your implementation of
concatenate
, you may only use the cons
function to build your result
list, and it should have linear runtime w.r.t. the total number of integers.
runlengthencode
: returns a runlength encoding of the argument list.
Runlength encoding stores each value together with the number of times it
appears (contiguously) — in this way, a value that is repeated multiple
times can be more efficiently represented. E.g.,
> (runlengthencode '(a a a a a b b b))
'((a 5) (b 3))
> (runlengthencode '(a a a b a a c c c b b))
'((a 3) (b 1) (a 2) (c 3) (b 2))
Your implementation should be tail recursive. You may assume that adjacent
elements can be compared for equality using equal?
.
runlengthdecode
: given a runlength encoded list argument, returns the
original list. E.g.,
> (runlengthdecode '((a 5) (b 3)))
'(a a a a a b b b)
> (runlengthdecode '((5 3) (12 2)))
'(5 5 5 12 12)
labelsexp
: returns a "labeled" version of the argument sexp. The labeling
is based on a simplified understanding of the Racket language, where a sexp
is one of:
42
x
, num
+
, *
, /
, or

, and followed by two argument sexps; e.g., (+ 4 5)
, (* x (+ 1 2))
(foo 42)
, (bar (+ 5 (foo 10)))
The labeled version of a sexp will enclose each of the original components
within a list with a label, which is one of int
, var
, arith
, op
,
funcall
, or name
. E.g., (indentation added for clarity)
> (labelsexp 42)
'(int 42)
> (labelsexp 'x)
'(var x)
> (labelsexp '(+ 1 2))
'(arith (op +) (int 1) (int 2))
> (labelsexp '(+ 1 (* x 2)))
'(arith (op +)
(int 1)
(arith (op *)
(var x)
(int 2)))
> (labelsexp '(foo 42))
'(funcall (name foo) (int 42))
> (labelsexp '(foo (+ 5 (foo 10))))
'(funcall (name foo)
(arith (op +)
(int 5)
(funcall (name foo) (int 10))))
You may find it quite helpful to use match
and quasiquoting (to construct
your labeled expressions). We've provided you with a bit of starter code.
We've provided you with tests for every exercise in the "mp1tests.rkt" source file. If you open the file you'll find that there are a bunch of definitions that looks like this:
(testcase "Integer exponentiation"
(checkequal? (iexpt 100 0) 1)
(checkequal? (iexpt 100 1) 100)
(checkequal? (iexpt 5 2) 25)
(checkequal? (iexpt 5 3) 125)
(checkequal? (iexpt 2 10) 1024))
This defines a test case (here, for the iexpt
function). The checkequal?
calls are assertions. If they fail, they will produce errors that looks like
this:

Integer exponentiation
. FAILURE
name: checkequal?
location: mp1test.rkt:8:11
actual: 2
expected: 1

You can run the tests by loading the file in DrRacket and evaluating it. Note that while passing all the tests is a good indication that your solutions are on the right track, we don't guarantee exhaustive test coverage, nor that passing all tests will results in 100% on the assignment! (E.g., we will be checking for tailrecursive implementations manually).
When you are done with your work, simply commit your changes and push them to our shared private GitHub repository. Please note that your submission date will be based on your most recent push (unless you let us know to grade an earlier version).