Honors 295 - Fall 2003
Programming 1: Symbols and Structure
Due by email to DD, by noon, Sept 19
|
|
Solutions
Description
This assignment has four parts, in increasing difficulty.
Don't try to do it all at once. Have you read The Little
Schemer? They're serious about the jelly stains.
When you get stuck, ask for help and take a break.
Your answers aren't due until Friday. So you can even ask
questions in class next week!
The file structure.scm, which we
went over in class, contains definitions to define and access
structures of parts and wholes. It creates two sample structures,
basic-doric-form and full-doric-form. You will
edit and extend this file and hand in your final result.
Here's what to do.
-
The first part of the homework is to write scheme expressions,
in terms of full-doric-form, name-of,
parts-of and car and cdr, that pick out
things for you:
-
the list of parts of the entablature (the entablature is the
first part of the doric form).
-
the name of the first part of the column (the column is the second
part of the doric form).
-
the list of parts of the cap (the cap is the first part of
the pedestal, the third part of the doric form).
Save your answers by defining them in a line in your answer scheme
file. For example, if we had asked as question 1.0 to give the
name of the second part of the doric form, you could answer by
(define a1.0 (name-of (car (cdr (parts-of
full-doric-form)))))
-
The second part of the homework is to create your own structure of
parts and wholes. You should create a structure for something
that you're reasonably familiar with. Don't worry about
reprsenting the whole thing. You might use a selection of parts
of the body, a selection of parts of the Rutgers campus, a
selection of parts of a computer or car, whatever. The only
requirements are that your whole should have at least ten
subparts, and the subparts should have four levels of
hierarchy. In other words, the whole should have a part one of
whose parts has a part of its own. (For example, the doric form
has a part, namely the column, one of whose parts, namely the
capital, has a part of its own, namely the abacus. The doric form
actually has five levels of hierarchy because the abacus
has parts, too.)
Save your answer this way:
(define a2 (complex-part ...))
-
Now you should be familiar with these structures. Start by
writing two functions, has-part? and get-part.
-
The expression (has-part? structure name) asks whether
one of the parts in the part-list of structure is called
name. So structure has to be a structure of
parts and wholes, and name should be a symbol. The
answer either true or false.
It's easiest to do this with a helper function. You can model
your helper function on member (from The Little
Schemer, page 23, and also one of the recursion examples), which does
something rather similar. Our implementation of
length2 with a helper function may give you a general
idea of how has-part? should call this helper
function.
-
The expression (get-part structure name) returns the
structure associated with the part of structure called
name. If structure has no part named
name you can return false. Again,
structure has to be a structure of parts and wholes,
and name should be a symbol.
Your answer for get-part will probably be a lot like
your answer for has-part?.
-
(Advanced.) Write two more functions, has-subpart? and
get-subpart.
-
The expression (has-subpart? structure name) asks
whether structure is related any way at all to a part
called name. In other words, name could be
the name of structure, or it could be the name of a
part of structure, or it could be the name of a part
of a part of structure, etc. So structure
has to be a structure of parts and wholes, and name
should be a symbol. The answer either true or
false.
Although this seems much more complicated than
has-part? the program doesn't have to be much longer.
The trick is that the program is recursive in two
places, rather than just in one. As your helper function
considers each part, it has to look inside it, rather than
looking directly at its name.
-
The expression (get-subpart structure name) returns the
structure associated with the subpart of structure called
name. If structure has no part named
name you can return false. Again,
structure has to be a structure of parts and wholes,
and name should be a symbol.
Your answer for get-subpart will probably be a lot like
your answer for has-subpart?.
295 Home