| CS 331 Spring 2013 > Assignment 6 |
Assignment 6 is due at 5 p.m. Tuesday, May 7. It is worth 20 points.
E-mail
answers to the exercises below to
ggchappell@alaska.edu,
using the subject
“PA6”.
ex6a.scm, from Exercise A,
and ex6b.fs, from Exercise B.
These files (or an archive file containing them)
should be attached to your e-mail message.In this exercise, you will write some simple Scheme functions involving lists.
Write Scheme code meeting the requirements outlined below.
Place all of this in the file ex6a.scm.
The file should define the following.
lastitem.
This should take a single argument, a list, and return its last item.
lastitem.
[Interactive Scheme]
> (lastitem '(1 8 2 5 1 6 3)) 3 > (lastitem '((1 2) 3 (4 5))) (4 5) > (lastitem '(1)) 1
lastitem
may produce an error message
if it is given an empty list
or an argument that is not a list.
(It will probably do this anyway.
In other words, you do not need to worry
about what lastitem
does if it is given something that does not have a last item.)last.evens.
This should take a single argument, a list,
and return a list containing items 0, 2, 4, etc.
evens.
[Interactive Scheme]
> (evens '(1 9 2 4 6 3 8)) (1 2 6 8) > (evens '(2 3)) (2) > (evens '(4)) (4) > (evens '()) () > (evens '((1 2) (3 4) (5 6) (7 8))) ((1 2) (5 6))
evens
may produce an error message if it is given
an argument that is not a list.
It should handle the empty list as above, however.#lang scheme”
will be added to your file.
You do not need to remove this line
(in fact, I would prefer that you leave it in).In this exercise, you will write code for a simple Forth word.
Write ANSI Forth code meeting the requirements outlined below.
Place all of this in the file ex6b.fs.
The file should define the following.
collcount.
This should take a single argument, a number,
and return a number.
If we call the argument \(n\),
then the return value should tell how many applications
of the Collatz function
(see Assignment 4 for details)
are required to take \(n\) to \(1\).
collcount.
[Interactive Forth]
1 collcount . 0 ok 2 collcount . 1 ok 3 collcount . 7 ok 4 collcount 5 collcount 6 collcount ok .s <3> 2 5 8 ok
ggchappell@alaska.edu