[previous] [up] [next]     [index]
Next: Development through Iterative Refinement Up: Mutually Referential Data Definitions Previous: Designing Functions for Mutually

Extended Exercise: More on Web Pages

With mutually referential data definitions we can represent Web pages in a more accurate manner than in section [cross-reference]. Here is the basic structure definition:

(define-struct wp (header body))
The two fields contain the two essential pieces of data in a Web page: a header and a body. The data definition specifies that a body is a list of words and Web pages:

A Web-page (short: WP) is a structure:

(make-wp h p) where h is a symbol and p is a (Web) document.

A (Web) document is either

  1. empty,
  2. (cons s p)
    where s is a symbol and p is a document, or
  3. (cons w p)
    where w is a Web page and p is a document.


Exercises Exercise 15.3.1

Develop the function size, which consumes a Web page and produces the number of symbols (words) it contains. Solution

Exercise 15.3.2


external

Develop the function wp-to-file. The function consumes a Web page and produces a list of symbols. The list contains all the words in a body and all the headers of embedded Web pages. The bodies of immediately embedded Web pages are ignored. Solution

Exercise 15.3.3

Develop the function occurs. It consumes a symbol and a Web page and determines whether the former occurs anywhere in the latter, including the embedded Web pages. Solution

Exercise 15.3.4

Develop the program find. The function consumes a Web page and a symbol. It produces false, if the symbol does not occur in the body of the page or its embedded Web pages. If the symbol occurs at least once, it produces a list of the headers that are encountered on the way to the symbol.

Hint: Define an auxiliary like find that produces only true when a Web page contains the desired word. Use it to define find. Alternatively, use boolean? to determine whether a natural recursion of find produced a list or a boolean. Then compute the result again. We will discuss this second technique, called backtracking, in the intermezzo at the end of this part. Solution



[previous] [up] [next]     [index]
Next: Development through Iterative Refinement Up: Mutually Referential Data Definitions Previous: Designing Functions for Mutually

PLT