Nov 3, 1997
Due: Nov 14
initialize
-- initialize this queue to be an empty queue
enqueue: x
-- add an object x to the rear of the queue
dequeue
-- dequeue an object from the front of the queue
and return it
head
-- return the object at the head of the queue
(without removing it)
do: block
-- for each element
in the queue, evaluate the block with that element as an argument
(analogous to the "do:" message to arrays and other kinds of collections)
printOn: aStream
-- the standard print message
ArrayQueue new
). How can this be fixed so that new queues are
initialized automatically? Explain how your code works. (And, if you are
still implementing, implement it.)
Write a short essay (1-2 paragraphs) comparing Smalltalk's approach to initializing objects with the approach in some other language with which you are familiar.
#(1 0 5 100 200) fastproduct
[^nil]
evaluates to a
block. Where does the block return to if it is evaluated?
body:
, which takes a block as an argument and is the body of
code to execute; execute
, which actually executes the loop;
and exit
, which exits the loop (perhaps prematurely).
ForLoop should also understand interval:
, which is an instance
of Interval, and gives the bounds. WhileLoop and RepeatLoop should also
understand test:
. For loops expect a block with an argument
for the body; while and repeat loops take a block with no arguments.
For example:
2 4 6
on the transcript. (Note that the loop is
terminated when i=6.)
The only tricky part of this question is implementing exit
.
The easiest way to do this is to save an 'exit block' at the beginning of
the execute
method. This exit block can just be
[^nil]
. If the loop object gets the exit message, it can just
evaluate this block, thus terminating the loop.
You can use the ordinary Smalltalk control structures in implementing your new control structures.
Finally, if you are so inclined, experiment with the graphics tools available in Smalltalk. A fun assignment is to implement the Smalltalk turtle (and colored turtle) -- see Assignment 4 in Fall 1994 Assignments and also the sample solution. You don't need to do any of these though.