CompSci 141 /
CSE 141 / Informatics 101 Winter 2014 | News | Course Reference | Schedule | Project Guide
| Code Examples
This webpage was adapted from Alex
Thornton’s offering of CS 141
CompSci 141 /
CSE 141 / Informatics 101 Winter 2014
Project #4
Due date and time: March 7, Friday, 11:59pm; No late submission will be accepted.
Introduction
For this project, you're
being asked to write six Scheme functions according to the given
specifications. This will give you the opportunity to practice functional
programming and, in addition, learn more about how to approach and solve
problems using recursion in functional programming languages.
While
there are many Scheme environments available (some free, others not), they tend
to differ, often in subtle ways. So, as a standard for this course, we will use
an environment called Racket, which the TAs will use for grading your project.
Racket is notable for its support of several different graduated subsets of
Scheme targeted at students, each one more complete than the previous one; we
will be using one of these for our work here. If you use another Scheme
environment, or if you have prior experience with standard Scheme, you will
discover some differences between full, standard Scheme and our chosen subset.
The best advice is to stick with Racket for this project.
Racket
is a Scheme environment built primarily for use in teaching programming. It
runs on all versions of Windows, Mac OS X, and various flavors of Unix and Linux. Racket is already installed on the machines
in the lab, and it can be downloaded free for your use at racket-lang.org. The latest version, as of
this writing, is 5.1.1, though new versions come out somewhat regularly; the
explanation below assumes that you're using that version. (Previous versions are
essentially the same.)
Installation
of Racket on Windows is a snap. (I haven't installed it on other operating
systems, but I presume it's just as simple on the others.) Just execute the
setup program and it will take care of everything. Once the installer is
finished, you'll find a folder called Racket in the Programs folder on your
Start menu. Select Racket from that menu, and off you go.
When
you first execute Racket, you'll be prompted to choose a language from a list.
Remember that Racket supports several different "strengths" of
Scheme, each allowing some features and disallowing others, and each including
a different combination of libraries and features. (If you're not prompted to
select a language, you can also select one at any time by going to the Language
menu and selecting Choose Language....) On the list of languages, under
Teaching Languages, click How to Design Programs and
then Advanced Student, then click OK. Finally, click the Run button near the
top-right corner of the main Racket window. Now you should be ready to roll!
Below
is a screenshot of Racket in action. (The version of Racket in the screenshot
is a previous version, called DrScheme, but one that
is more or less the same as the current one.)

Project
Description
Below
are a set of Scheme functions that you are required to write for this
project. In general, you are not
permitted to make assumptions about the arguments to each function other than
the assumptions listed in the description of each function. For example, unless
explicitly stated, you may not assume that all lists will be simple — a simple
list is one with no sublists.
Make
sure that your code works in Racket with the Advanced Student language
level selected. Beyond that, we're also staying within an even more restricted
subset of Scheme. You may only use the following predefined Scheme functions or
constructs in your solution:
define lambda
cond
else empty empty? first rest
cons list
list? = equal?
and or not
+ - *
/ < <=
> >=
Some
of the functions below cannot be written easily without other
"helper" functions. Turn in all of your helper functions along with
the ones you are to write. You may reuse helper functions in more than one of
your solutions if you'd like, though it isn't required. Similarly, you may call
your solution to one of your functions in your solution to another.
The
Advanced Student language level in DrScheme provides
two equivalent ways of describing a list: using the list construct or a
short-hand version consisting only of the lists elements surrounded by
parentheses. For example, a list containing the numbers 1, 2, 3, 4, and 5 can
be written in one of two ways:
(list 1 2 3 4 5)
'(1
2 3 4 5)
These
two notations can be a little confusing, because they sometimes require quoting
in different places. For example, a list of symbols x, y, and z
would look like this in each of the supported styles:
(list 'x 'y 'z)
'(x y z)
For
your work, either of these styles is fine when writing your functions. When
using the Advanced Student language level, as we will be in this
project, the list construct is used to describe lists returned as
output.
Here
are the six functions you're being asked to write. Each includes a brief set of
examples that shows what its output should be in a few cases; note that these
examples are not intended to be a complete set of tests for each function, so
you may want to develop a few extra examples. (Remember to pay special
attention to the base case for each function, which is not always listed in the
examples, but whose answer you should be able to deduce from the description of
the problem.)
(fourth-element L)
The fourth-element function takes a list L and returns its fourth element. Of
course, not all lists have four or more elements; if L is a list that doesn't,
the empty list should be returned. L is not necessarily simple, but you
shouldn't handle sublists differently from other
elements (i.e., if the fourth element is a sublist,
return the whole sublist).
Examples:
(fourth-element
'(a b c d e)) => d
(fourth-element
'(x (y z) w h j)) => h
(fourth-element
'((a b) (c d) (e f) (g h) (i j))) => (list 'g 'h) or ‘(g h)
(fourth-element '(a b c)) => empty
(list-length L)
The list-length function takes a list L and returns the number of elements in
the list.
Examples:
(list-length '(a b
c)) => 3
(list-length '(a (b
c) d e f)) => 5
(count-matches S L)
The count-matches function takes a symbol S and a simple list L of symbols and returns the number of times that S occurs in L.
Examples:
(count-matches 'f
'(a b c d e f g)) => 1
(count-matches 'b
'(a b a b a b a)) => 3
(count-matches 'x '(a
b c)) => 0
(my-append L1 L2)
The my-append function takes two lists, L1 and L2, and
returns the concatenation of L1 and L2. ("Concatenation" means to
stick one on the end of the other.) Note that concatenation is not the
same thing as what "cons" does to two lists.
Examples:
(my-append '(a b)
'(c d)) => (list 'a 'b 'c 'd)
(my-append '() '(a
b)) => (list 'a 'b)
(my-append '(a b)
'()) => (list 'a 'b)
(is-increasing? L)
The is-increasing? function takes a simple list L of
numbers and returns true if the numbers in the list are increasing as you read
them from beginning to end, and false if they aren't. We'll define
"increasing" according to the mathemtical
definition of the word, where the numbers are increasing so long as they never
decrease. (This is as opposed to what you might call "strictly
increasing," where every number has to be bigger than the previous one.)
As special cases, we'll consider one-element and empty lists to be increasing.
Examples:
(is-increasing?
'(1 2 3)) => true
(is-increasing?
'(3 2 1)) => false
(is-increasing?
'(1 2 2 3 4 4 5)) =>
true
(is-increasing?
'(1)) => true
(is-increasing?
'()) => true
(remove-duplicates L)
The remove-duplicates function takes a simple list L and returns a new list
with all of the duplicate objects in L removed.
Examples:
(remove-duplicates
'(1 2 3)) => (list 1 2 3)
(remove-duplicates
'(1 2 1 4)) => (list 2 1 4) -or- (list 1 2 4)
(remove-duplicates
'(3 3 3 3 3)) => (list 3)
Deliverables
Put
all of your solutions into a single file called lastname_project4.scm. Submit
this file and no others. We must be able to read this file directly into the Racket
environment to test it, so don't write the procedures in Microsoft Word or
another format.
Please
include a comment at the top of the file that lists your name and student ID.
Comments in Scheme begin with a semicolon character — though, by convention, we
often use two of them in a row, so they're easier to see. Everything that
follows a semicolon on any line is ignored by the Scheme interpreter.
Follow
this link for a discussion of how to submit
your project. Remember that we do not accept paper submissions of your
projects, nor do we accept them via email under any circumstances.
Adapted from a similar document from CS 141 Winter 2013 by Alex
Thornton,