Structures for Complex Web Pages

Whenever we have data structures which contain (say) exactly three elements, a structure is a natural candidate. As an example, consider the Complex Web Page from the reader (and recent lectures, but not earlier lectures):
  • A Complex Web Page is
    (list marker contents)
    where marker is a symbol, and contents is a Web List.
  • A Web List is:
  • null,
  • (cons s wl), where w is a symbol and wl is a Web List,
  • (cons ewp a-wp), where ewp is a Web Page and a-wp is a Web List.
  • Since a Complex Web Page always has three items, we can declare a corresponding structure for it:
    (define-structure (cwp marker contents))
    
    This statement creates some new Scheme functions for us automatically:
  • make-cwp to make such a structure,
  • functions cwp-marker and cwp-contents to extract the desired data from a structure so created, and
  • cwp? which takes an object, and returns a Boolean value, depending on whether the object was created with make-cwp.
  • An example would help, about now:

    (define oaks (make-cwp 'oak
                           (list 'This 'is 'a 'page 'about 'oak 'leaves!)))
    (define elms (make-cwp 'elm
                           (list 'This 'is 'a 'page 'about 'elm 'leaves,
                                 'but 'not 'oak leaves.)))
    (define allTrees (make-cwp 'Botany 
                               (list 'here 'are 'some 'pages 'with oaks 'and elms))
    
    (cwp-marker allTrees)
    (cwp-contents allTrees)
    (cwp? oaks)
    (cwp? 4)
    
    Now, using the above structure, write a template for Complex Web Pages and Web Lists. Our data description for a Complex Web Page now looks simple:

    A Complex Web Page is
    (make-cwp marker contents),
    where marker is a symbol, and contents is a Web List.

    What about using structures for the Web List itself? Instead of having one structure trying to handle several different cases, we can define a different structures for each interesting option of Web Lists. For the option starting with symbol, we can use:

    (define-structure (wls sym rest))
    
    And for the line starting with an embedded web page,
    (define-structure (wle ewp rest))
    
    Our Data Definition for Web Lists now looks liks:
    A Web List is either
  • null,
  • (make-wls sym rest),
    where sym is a symbol and rest is a Web List, or
  • (make-wle ewp rest),
    where ewp is a Complex Web Page, and rest is a Web List.
  • What does a template look like? How would you actually create a Web List from scratch? Write a function to count how many time the symbol 'oak occurs in a Complex Web Page. Notice how (cwp-contents acwp) replaces (car (cdr acwp)), which is easier to read and less error-prone.