Teaching
2500 F '11
 
Assignments
The Hand In
Set 1
Set 2
Set 3
Set 3h
Set 4
Set 4h
Set 5
Set 5h
Set 6
Set 7
Set 6h
Set 8
Set 7h
Set 9
Set 8h
Set 10
Set 9h
Set 11
Set 10h
Set 12

Problem Set 6h

Due date: 10/24 @ 11:59pm


Honors-section homework

HtDP Problems

14.2.1-6, 17.2.2, 17.6.1, 21.2.1, 21.2.2, 21.2.3


X-Expressions

You may use the Beginning Student Language with List Abbreviations for this problem

Here is the data definition for X-expressions:

#| X-expressions:

   Xexpr is one of: 
   -- (cons Symbol (cons LOA Xbody))
   -- (cons Symbol Xbody)

   Xbody is one of: 
   -- empty
   -- (cons String Xbody)
   -- (cons Xexpr Xbody)

   LOA is one of: 
   -- empty 
   -- (cons (list Symbol String) LOA)

   Note: (list Symbol String) is called an Attribute. 
   Furthermore, (list 'a "some text") is called an a-Attribute 
   and "some text" is its value. 
|#

X-expressions are used to represent XML information within the BSL language

The "teachpack" more-io.ss provides the function read-xexpr for reading an entire XML files as an X-expression. To use the teachpack, save it in the same directory as your code for the problem set. In DrRacket, select the Language > "Add Teachpack..." menu item, then "Add Teachpack to List...", then navigate to the location of more-io.ss and add it.

Before submitting, any use of read-xexpr must be commented out.

Here is a sample XML piece:

	<week>
	 <assignment>
	  hello
	  <syllabus week="2" src="sample2.xml" />
	  <syllabus week="1" src="sample1.xml">
	   world
	  </syllabus>
	 </assignment>
	 done
	</week>

Exercise 1: Represent it as an X-expression to practice your data representation skills.

Exercise 2: Your task is to design four functions:

  1. find-srcs, which consumes the name of an XML file (a file whose extension is .xml and whose content is an XML expression) and produces the list of all the values of src attributes in the file.
  2. xexpr-find, which consumes an X-expression and produces the list of all the values of src attributes.
  3. file-depth, which determines the depth, also called complexity, of the XML in the file of the given name.

    If you have at most one start tag per line and if you indent one more space every time you use an open tag without closing preceding tags, the depth of a piece of XML is the maximal number of spaces between a tag and the left border.

  4. xexpr-depth, which consumes an X-expression and determines its depth.

You should notice that two of the functions are one-line definitions, if you use function composition to define them. For your entertainment, this assignment comes with the XML file 6h.xml, which stores information about this assignment. You may use it to run your program (not test). For testing, you must create a simpler file than this.

Exercise 3: The last problem required the design of the functions: xexpr-find and xexpr-depth. Because the description of the input requires three data definitions, those of you who designed the functions according to the design recipe came up with three mutually interconnected functions (plus perhaps some auxiliary functions).

The first step to editing such clusters of functions after they have been properly designed and corrected is to re-organize them into a single function, just like the problem statement (or your boss's request) states for both of the two functions in this problem. Do so.

The reason for organizing code in this manner is to create an organizational structure for future readers. If xexpr-find or xexpr-depth occur in the context of a large module or program that some reader wishes to understand, he may just read the purpose statement and ignore the internal nest of local functions during a first pass. This provides a first overview of the module and puts everything in context. After that, the reader can "drill" down wherever needed.

Exercise 4: XML experts refer to (the interpretation of) an Xexpr as an element. Design the function xexpr-elements, which consumes an X-expression x and symbol t and extracts a list of all elements whose tag is t. Note that an element with tag t may occur nested inside of some other element with the same tag.

Exercise 5: Abstract over xexpr-find, xexpr-depth, and xexpr-elements. That is, create a single function that consumes and processes an Xexpr and demonstrate that the three functions are simple "instances" of this function.

Hint: Start from the template for all three functions. (Remember that since all three functions process the same kind of input data their templates---i.e., their organizational skeletons---are identical. Then create a function that consumes appropriate "combinators" and "base values" for the various clauses that must be distinct.

Knowledge: This abstraction is at the core of many XML processing languages and thus shows up at the heart of many XML libraries.

The Snake Game, Revisited

Exercise 6: Edit the Snake game we developed in class to use the new ISL abstractions (page 313).


last updated on Tue Nov 29 19:02:43 EST 2011generated with Racket