CS 331 Spring 2013  >  Assignment 6

# CS 331 Spring 2013 Assignment 6

Assignment 6 is due at 5 p.m. Tuesday, May 7. It is worth 20 points.

## Procedures

E-mail answers to the exercises below to ggchappell@alaska.edu, using the subjectPA6”.

• Your answers should consist of two files: 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.
• I may not read your homework e-mail immediately. If you wish to discuss the assignment (or anything else) with me, send me a separate message with a different subject line.

## Exercises (20 pts total)

### Exercise A — Basic Scheme

#### Purpose

In this exercise, you will write some simple Scheme functions involving lists.

#### Instructions

Write Scheme code meeting the requirements outlined below. Place all of this in the file ex6a.scm.

The file should define the following.

• Procedure lastitem. This should take a single argument, a list, and return its last item.
• Here is a sample DrRacket session that uses lastitem.

[Interactive Scheme]

> (lastitem '(1 8 2 5 1 6 3))
3
> (lastitem '((1 2) 3 (4 5)))
(4 5)
> (lastitem '(1))
1
• Procedure 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.)
• You may not use the standard Scheme function last.
• Procedure evens. This should take a single argument, a list, and return a list containing items 0, 2, 4, etc.
• Here is a DrRacket session that uses 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))
• Procedure 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.

#### Other Requirements & Notes

• Your file must be properly commented, with filename, author, date, and purpose, as usual.
• You may define any other symbols you wish.
• Your functions do not need to do any type checking.
• If you use DrRacket, then a line reading “#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).
• There will be no test program posted. Test your code!

### Exercise B — Basic Forth

#### Purpose

In this exercise, you will write code for a simple Forth word.

#### Instructions

Write ANSI Forth code meeting the requirements outlined below. Place all of this in the file ex6b.fs.

The file should define the following.

• Word 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$$.
• Here is a sample gForth session that uses 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


#### Other Requirements & Notes

• Your file must be properly commented, with filename, author, date, and purpose, as usual.
• You may define any other words you wish.
• There will be no test program posted. Test your code!

CS 331 Spring 2013: Assignment 6 / Updated: 1 May 2013 / Glenn G. Chappell / ggchappell@alaska.edu