(define-structure (cwp marker contents))This statement creates some new Scheme functions for us automatically:
make-cwp
to make such a structure,
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:
null
,
(make-wls sym rest)
,sym
is a symbol and rest
is a Web List, or
(make-wle ewp rest)
, ewp
is a Complex Web Page, and rest
is
a Web List.
'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.