CSE 413 19wi Assignment 1 - Racket Warmup
Due: Tuesday, January 15, 2019 by 11 pm. You should use Gradescope to submit your assignment in two parts, corresponding to the written and programming parts below. Details are given in the "What to hand in" section at the bottom of the assignment.
All of these problems must be done without using side effects (i.e.
no redefining variables or using set!
) and by using recursion
instead of iteration constructs like do
. Be sure to test
your functions on various cases, including empty lists, simple lists
with no sublists, complex lists, etc.
Some of the problems can be answered by just typing expressions into a Racket interpreter and transcribing the results. Feel free to use DrRacket to check your results, but, even though you might "get the points", you won't learn what you need if you don't actually do the problems.
For results that are lists or symbols (names), you shold precede
them with a quote (as printed by the current version of Racket), i.e.,
you should write '(a b)
for the result of
(list 'a 'b)
.
Part I. Written problems
1. (a) Draw a "boxes 'n arrows" diagram showing the effect of executing the following Racket statements in the following order.
(define lst '(a b c)) (define another '((x) y)) (define q1 (list lst another)) (define q2 (cons lst another))
(b) What are the values of q1
and q2
if these are printed?
2. Suppose we execute the following Racket statements
(define lst '(p q r)) (define more '((a b) c)) (define app (append lst more)) (define rev (reverse more))
For each of the following, give the value that results from evaluating the given expression.
(a) (cdr lst)
(b) (cddr lst)
(c) (cadr more)
(d) app
(e) rev
(f) (cons more rev)
Part II. Programming Problems.
Write and test Racket functions to solve the following problems. In
some cases you may find it useful to write additional helper
functions besides just the functions requested. You should save all
of your function definitions in a single source file
named hw1.rkt
. In DrRacket you can
use File->Save definitions as...
to create a file
that contains your function definitions.
The beginning of your file must contain comments giving your name
and UW netid. Then, following the line containing #lang racket
your file must contain the following separate line, written exactly as shown:
(provide (all-defined-out))(This provides an interface needed for the grading software to execute your code, but is not something you need to worry about as long as it is written correctly.)
You may assume that all arguments have appropriate types (numbers, lists, tree nodes, etc.) and appropriate values (for example, if you are asked to compute something like n!, you can assume the argument will not be negative). You do not need to add explicit checks for such error cases. However, you should not make unreasonable assumptions like assuming that lists are not empty when that is not required by the problem.
The number of possible combinations (subsets) of k things taken from a set of n items is given by the formula C(n,k) = n! / (k! (n-k)!). (where ! is the factorial function) Write a function
(comb n k)
to compute C(n,k). You may use any recursive implementation of factorial that you like.Write a function
zip
that takes two lists as arguments and returns a single list whose elements are taken alternatively from the original lists. For example,(zip '(a b c) '(x y z))
should evaluate to'(a x b y c z)
. If the lists have different lengths, the remaining unpaired elements of the longer list should appear at the end, e.g.,(zip '(1 2) '(w x y z))
should evaluate to'(1 w 2 x y z)
.
-
Write a function
unzip
that takes a list as an argument and returns a list containing two lists that have alternating elements from the original list. For example,(unzip '(a b c d e f))
should evaluate to'((a c e) (b d f))
. If the original list has an odd number of elements, the extra element at the end may appear at the end of either of the resulting lists.
-
If a list contains multiple copies of the same element in succession, the list can often be stored more compactly using a run length encoding, in which the repeated element is given just once, preceded by the number of times it is repeated. Write a function
expand
that takes a list of elements and frequencies and expands them into a simple list. For example, the result of(expand '(a (3 b) (3 a) b (2 c) (3 a))
should be'(a b b b a a a b c c a a a)
.
-
A binary tree can be represented as a list in many ways. One way is to represent each node in the tree as a list of three items containing the node value and left and right subtrees, with the empty list
()
being used to represent an empty tree or subtree. For example,()
is an empty tree,(1 () ())
is a single node containing the value 1 and empty left and right subtrees, and(1 (2 () ()) (3 () ()))
is a tree with root 1, a left subtree containing the value 2, and a right subtree with the value 3.To make it easier to work with data structures in classic Scheme or Racket, one convention is to define simple functions to construct and access elements of the data structure. For a binary tree, we can define the constructor function
(node value left right)
as(list value left right)
. Nothing changes underneath, but we can then use thisfunction when we construct list nodes to make the intent more apparent to the reader instead of using list
directly. (We will later see a different way to define such data structures in Racket, but for this assignment please encode binary trees this way.)-
Write functions
value
,left
, andright
that return respectively the node value, left subtree and right subtree from a tree node as defined above. (Hint: these are simple operations likecadr
.) Use these functions in the rest of this problem. - Write a function
(size tree)
that returns the number of non-null nodes in the given tree. - Write a function
(contains item tree)
that returns true ifitem
appears as a node value somewhere in the tree, otherwise false. Useequal?
to compare items. - Write a function
(leaves tree)
that returns a list of the values in the leaves of the tree in order from left to right. For example, in the second example tree above, the result ofleaves
would be the list(2 3)
. - Write a function
(isBST tree)
that returns true if the tree is a Binary Search Tree and false otherwise. You may assume the tree given as an argument contains only numbers as values.
Note that the
value
s stored in the tree nodes can be any Racket expression, not just numbers or symbols, unless the problem states otherwise. -
Write functions
What to Hand In
Use Gradescope to submit your solutions to this assignment in two parts.
For the problems in part I please turn in a pdf file
named hw1.pdf
with your answers. This can be a scanned
handwritten document if that is convenient, as long as it is clear
and legible.
Gradescope's web site has several instructional videos and help pages to outline the process, and this guide
has specific information about scanning and uploading pdf files containing
assignments.
- Unreadable solutions cannot be graded--no blurry photos, poor contrast, or illegible handwriting, please.
- Type-written solutions are encouraged but not required.
- If possible, don't split the solution to a problem across a page break.
For part II, turn in the hw1.rkt
file containing your
Racket functions. Use comment lines (starting with ;;
)
to label your code so we can easily find the solutions to each
problem. (But don't overdo it. Excess clutter is just as bad
as unlabeled code.) Be sure that your file contains the
(provide ...)
line described at the beginning
of part II.
Also submit a text file
named hw1demo.rkt
containing the transcript of a Racket
session demonstrating the results produced by your functions for
suitable test cases. This should contain enough to show that your
functions work, but no more than necessary to do that. Part of your
grade will be determined by the quality of the test cases you use for
this. (Don't worry if there are some minor typos in the transcript -
just include a comment (;;
line) to indicate anything we
should ignore.) To save the transcript in the DrRacket interactions
window to a file, use File > Save Other... > Save
Interactions as Text...
.
Computer Science & Engineering University of Washington Box 352350 Seattle, WA 98195-2350 (206) 543-1695 voice, (206) 543-2969 FAX
Comments to admin