Fundamentals of Computer Science I (CS151 2003F)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
Contents:
Resources:
Create a new stack and name it tags. Push onto this stack the
opening tag "<html>". Next, push
"<head>", the tag that begins the header of a hypertext
document, and then "<title>". Now pop the stack. The
tag that appears is the one that must be matched first by a closing tag in
order for the tags to be correctly nested. Pop the stack two more times and
confirm that the stack is a last-in, first-out
data structure.
Documents on the World Wide Web usually contain special strings, called tags, that serve as instructions to the browser about what the document contains, how it is structured, and how the text should be displayed. In many cases, tags occur in pairs: The opening tag marks the beginning of a region of text that constitutes some natural unit within the document structure or should be displayed in some special way, and closing tag marks the end of that region.
Netscape and other browsers use a stack of tags like this one -- a stack
containing tags that must eventually be matched but have not been matched
yet -- to determine whether the HTML document to be displayed is
correctly constructed. Write a Scheme procedure
correctly-nested? that takes a list of HTML
opening and closing tags and determines whether they are correctly nested.
You need not handle singleton tags.
> (correctly-nested? '("<html>" "<head>" "<title>" "</title>"
"</head>" "<body>" "<b>" "</b>" "</body>" "</html>"))
#t
> (correctly-nested? '("<html>" "<head>" "</html>" "</head>"))
#f
Some authors add another operation to the definition of the stack ADT:
size, which returns the number of elements in the stack. Extend the
Scheme implementation of make-stack above so that the stacks
it constructs will accept the message ':size and perform this
operation when it is received.
Some authors add another operation to the definition of the stack ADT:
purge, which eliminates all the elements in the stack. Extend
the Scheme implementation of make-stack so that the stacks
it constructs will accept the message ':purge! and perform
the corresponding operation.
The current implementation of make-stack does not verify
that the stack is called correctly. For example, one could provide another
parameter to ':pop!, even though no such parameters should
be permitted.
Extend the implementation of make-stack so that each operation
checks that it is associated with the correct number of additional parameters.
In class, I claimed that stacks provide a useful way to handle deep recursion. In particular, to sum all the values in a number tree, you create a recursive kernel of two parameters, (1) a stack of all the subtrees left to sum and (2) the sum of values seen so far. At every step, the kernel grabs the top of the stack. If the top element is a pair, it pushes the two subtrees. Otherwise, it adds the value (which should be a number) to the sum so far. It stops when the stack is empty.
Implement this variant of sum-of-number-tree.
Tuesday, 9 December 2003 [Samuel A. Rebelsky]
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
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 Tue Dec 9 14:10:28 2003.
The source to the document was last modified on Tue Dec 9 14:10:26 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/stacks.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby