# Evaluation of Homework 2

I read through your code and tested it when appropriate. You can find my comments in the file as lines that begin with a semicolon and SR.

If I was unlikely to give you a B or better, I gave you a "Satisfactory" instead. I will not be so generous on the next assignment.

It seems that very few of you bothered to read the instructions for laboratory writeups. If you had read those instructions, you would have noted that

• Your writeups needed to include tests of the various procedures you created. Often, the tests you choose tell me as much as the code you write.

Some notes on procedure documentation:

• Make sure to name the value your procedure produces.
• Some of you need to work harder on writing your postconditions. The postconditions should really specify as much useful as you can about the result.
• When your lines are longer than 70-or-so characters, please hit return and continue your comment on the next line.

I noted that some of you had answers that looked very different. My guess is that you chose the strategy of having one person answer some questions and another person answer other questions. You learn more if you work together and choose a uniform format for your answers.

When you test your procedures, please consider whether or not the results are reasonable. For example, one of you computed that the volume of a 16" softball was over 4 million cubic inches.

You should think about composing steps into a single expression. For example, many of you wrote something like the following for find the volume of a 12" softball:

```> (sphere-circ->radius 12)
1.910828025477707
> (sphere-volume 1.910828025477707)
29.21010994360826
```

Here's what I'd prefer

```> (sphere-volume (sphere-circ->radius 12))
29.21010994360826
```

## The `type` procedure

A few of you wanted to find a way to do a knowledge dump about numbers, such as it's exact and rational and an integer and odd. Since there's a complex/real/rational/integer hierarchy, I wouldn't worry about saying more than one of those, and would recommend the most specific. I also wouldn't worry about the even/odd split. However, I agree that it's useful and interesting to know not only what kind of number it is but also whether it is represented exactly or inexactly.

Here's one simple mechanism

```(cond
((and (integer? val) (exact? val)) 'exact-integer)
((and (integer? val) (inexact? val)) 'inexact-integer)
((and (rational? val) (exact? val)) 'exact-rational)
((and (rational? val) (inexact? val)) 'inexact-rational)
...
(else 'unknown))
```

Here's an alternative:

```(cond
((integer? val) (if (exact? val) 'exact-integer 'inexact-integer))
((real? val) (if (exact? val) 'exact-real 'inexact-real))
...
(else 'unknown))
```

If you really wanted to build up a long description (say, in a list), I'd recommend creating helper procedures. Here's one example in which each helper procedure takes two parameters, the value and a list of descriptors, and adds a descriptor to the list if the appropriate predicate holds.

```(cond
((number? val) (describe-number val))
...
(else 'uknown))

;;; Procedure:
;;;   describe-number
;;; Parameters:
;;;   val, a number
;;; Purpose:
;;    Describes the type of val.
;;; Produces:
;;;   Description, a list of symbols.
;;; Preconditions:
;;;   [None]
;;; Postconditions:
;;;   desc contains a list of symbols that describe val.
;;;   if desc contains that symbol x, then (x? val) holds.
;;;   desc can only contain the symbols integer, rational
;;;     real, complex, exact, and inexact.
(define describe-number
(lambda (val)
(check-integer val
(check-rational val
(check-real val
(check-complex val
(check-exact val
(check-inexact val
null))))))))

;;; Procedures:
;;;   check-integer
;;;   check-rational
;;;   check-real
;;;   check-complex
;;;   check-exact
;;;   check-inexact
;;; Parameters:
;;;   val, a number
;;;   desc, a list of symbols
;;; Purpose:
;;;   Determines whether val is of the specified type and, if
;;;   so, adds a symbol corresponding to that type to the front
;;;   of desc.
;;; Preconditions:
;;;   desc contains only valid descriptive symbols (as in check-number).
;;; Postconditions:
;;;   desc contains only valid descriptive symbols.
;;;   If val is the appropriate type, desc now contains that type.
(define (check-integer val desc)
(if (integer? val) (cons 'integer desc) desc))

(define (check-rational val desc)
(if (rational? val) (cons 'rational desc) desc))

(define (check-real val desc)
(if (real? val) (cons 'real desc) desc))

(define (check-complex val desc)
(if (complex? val) (cons 'complex desc) desc))

(define (check-exact val desc)
(if (exact? val) (cons 'exact desc) desc))

(define (check-inexact val desc)
(if (inexact? val) (cons 'inexact desc) desc))

```

## History

Thursday, 26 September 2002

• Created.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Dec 4 08:45:46 2002.
The source to the document was last modified on Fri Sep 27 08:28:36 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Homework/notes.02.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu