Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'Higher-order Functions
18.  Web related higher-order functions

We finish our coverage of higher-order functions with a number of examples from the web domain.

18.1 HTML mirror generation18.5 HTML element modifications
18.2 HTML mirror usage examples18.6 The function simple-html-table
18.3 Making tables with the real mirror18.7 The XHTML mirror in LAML
18.4 Tables with higher-order functions18.8 Generation of a leq predicate from enumeration
 

18.1.  HTML mirror generation
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In this section we will, in a principled way, show how to generate simple HTML mirror functions in Scheme. Please notice that the HTML mirror functions in LAML are more sophisticated and elaborate than the ones discussed here.

There are three different cases to consider: double tag elements, single tag elements, and tags that can be both single and double.

The higher-order functions generate-double-tag-function and generate-single-tag-function are the top level functions. They rely on a couple of other functions, which we program in Program 18.2 - Program 18.4.

1
2
3
4
5
6
7
(define (generate-double-tag-function tag-name)
  (lambda (contents . attributes)
    (double-tag tag-name contents attributes)))

(define (generate-single-tag-function tag-name)
  (lambda attributes
    (single-tag tag-name attributes)))
Program 18.1    The two higher-order functions for the HTML mirror generation.

1
2
3
4
5
6
7
(define (single-tag name attributes)
 (start-tag name attributes))

(define (double-tag name contents attributes)
 (string-append (start-tag name attributes)
                (as-string contents)
                (end-tag name)))
Program 18.2    Functions that generate single and double tags.

The functions start-tag and end-tag are used in Program 18.2 and implemented in Program 18.3.

1
2
3
4
5
6
7
8
(define (start-tag kind attributes)
  (if (null? attributes) 
      (string-append "<" kind ">")
      (let ((html-attributes (linearize-attributes attributes)))
         (string-append "<" kind " " html-attributes " >"))))

(define (end-tag kind)
  (string-append "</" kind ">"))
Program 18.3    Functions that generate individual single and double tags.

The missing aspect at this point is the attribute handling stuff. It is made in Program 18.4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(define (linearize-attributes attr-list)
  (string-append  
    (linearize-attributes-1
      (reverse attr-list) "" (length attr-list))))

(define (linearize-attributes-1 attr-list res-string lgt)
  (cond ((null? attr-list) res-string)
        ((>= lgt 2) 
          (linearize-attributes-1 
           (cddr attr-list)
           (string-append 
            (linearize-attribute-pair
             (car attr-list) (cadr attr-list)) " " res-string)
           (- lgt 2)))
        ((< lgt 2) 
          (error "The attribute list must have even length"))))

(define (linearize-attribute-pair val attr)
  (string-append (as-string attr)
                  " = " (string-it (as-string val))))
Program 18.4    Functions for attribute linearization.

Recall that property lists, as passed to the function linearize-attributes in Program 18.4 have been discussed in Section 6.6.

There are several things to notice relative to LAML. First, the HTML mirror in LAML does not generate strings, but an internal representation akin to abstract syntax trees.

Second, the string concatenation done in Program 18.1 through Program 18.4, where a lot of small strings are aggregated, generates a lot of 'garbage strings'. The way this is handled (by the render functions in LAML) is more efficient, because we write string parts directly into a stream (or into a large, pre-allocated string).

You will find more details about LAML in Chapter 26 and subsequent chapters.

 

18.2.  HTML mirror usage examples
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

Let us now use the HTML mirror generation functions, which we prepared via generate-double-tag-function and generate-single-tag-function in Section 18.1.

The example assumes loading of laml.scm and the function map-concat, which concatenates the result of a map application.

The real mirrors use implicit (string) concatenation

As noticed above, there some differences between the real LAML mirror functions and the ones programmed in Section 18.1. The functions from above require string appending of such constituents as the three tr element instances in the table; This is inconvenient. Also, the mirror functions from above require that each double element gets exactly one content string followed by a number of attributes. The real LAML mirror functions accept pieces of contents and attributes in arbitrary order (thus, in some sense generalizing the XML conventions where the attributes come before the contents inside the start tag). Finally, there is no kind of contents nor attribute validation in the mirror functions from above. The LAML mirror functions validate both the contents and the attributes relative to the XML Document Type Definition (DTD).

Expression

Value

(let* ((functions
         (map generate-double-tag-function 
              (list "table" "td" "tr")))
       (table (car functions))
       (td (cadr functions))
       (tr (caddr functions)))
 (table
  (string-append
   (tr 
     (map-concat td (list "c1" "c2" "c3"))
     'bgcolor "#ff0000")
   (tr
     (map-concat td (list "c4" "c5" "c6")))
   (tr
     (map-concat td (list "c7" "c8" "c9"))))
  'border 3))
<table border="3">
   <tr bgcolor="#ff0000">
     <td> c1 </td>
     <td> c2 </td> 
     <td> c3 </td>  
   </tr>
   <tr> 
     <td> c4 </td> 
     <td> c5 </td>
     <td> c6 </td>
   </tr>
   <tr> 
     <td> c7 </td>
     <td> c8 </td>
     <td> c9 </td>
   </tr>
</table>
Same as above
c1 c2 c3
c4 c5 c6
c7 c8 c9
Table 18.1    An example usage of the simple HTML mirror which we programmed on the previous page. The bottom example shows, as in earlier similar tables, the HTML rendering of the constructed table. The map-concat function used in the example is defined in the general LAML library as (define (map-concat f lst) (apply string-append (map f lst))). In order to actually evaluate the expression you should load laml.scm of the LAML distribution first.

To show the differences between the simple mirror from Section 18.1 and the real mirror we will show the same example using the XHTML mirror functions in Section 18.3.

 

18.3.  Making tables with the real mirror
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

The real mirror provide for more elegance than the simple mirror illustrated above

Here we will use the XHTML1.0 transitional mirror

In the example below there is no need to string append the tr forms, and there is no need to use a special string appending mapping function, like map-concat from Table 18.1. Attributes can appear before, within, or after the textual content. This makes the HTML mirror expression simpler and less clumsy. The rendering result is, however, the same.

Expression

Rendered value

(table
  'border 3
   (tr 
     (map td (list "c1" "c2" "c3"))
     'bgcolor "#ff0000")
   (tr
     (map td (list "c4" "c5" "c6")))
   (tr
     (map td (list "c7" "c8" "c9")))
)
<table border = "3">
  <tr bgcolor = "#ff0000">
   <td>c1</td>
   <td>c2</td>
   <td>c3</td>
  </tr> 
  <tr>
   <td>c4</td> 
   <td>c5</td> 
   <td>c6</td>
  </tr> 
  <tr>
    <td>c7</td>
    <td>c8</td>
    <td>c9</td>
  </tr>
</table>
Same as above
c1 c2 c3
c4 c5 c6
c7 c8 c9
Table 18.2    A XHTML mirror expression with a table corresponding to the table shown on the previous page and the corresponding HTML fragment. Notice the absence of string concatenation. Also notice that the border attribute is given before the first tr element. The border attribute could as well appear after the tr elements, or in between them.

You might think, that the example above also could be HTML4.01. But, not quite, in fact. In HTML4.01 there need to be a tbody (table body) form in between the tr instances and the table instance. Without this extract level, the table expression will not be valid. Try it yourself! It is easy.

[How, you may ask. In Emacs do M-x set-interactive-laml-mirror-library and enter html-4.01. Then do M-x run-laml-interactively. Copy the table expression from above, and try it out. You can shift to XHTML1.0 by M-x set-interactive-laml-mirror-library and asking for xhtml-1.0-transitional, for instance. Then redo M-x run-laml-interactively. Be sure to use xml-render on the result of (table ...) to make a textual rendering. ]

 

18.4.  Tables with higher-order functions
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In the context of higher-order functions there are even better ways to deal with tables than the one shown in Table 18.2 from Section 18.3.

The table expression in the last line in Table 18.3 shows how.

Instead of explicit composition of td and tr elements we can use a mapping to apply tr to rows and td to elements

Expression

Value

(define rows 
  '(("This" "is" "first" "row")
   ("This" "is" "second" "row")
   ("This" "is" "third" "row")
   ("This" "is" "fourth" "row"))
)

(table 'border 5
 (gmap
   (compose tr (gmap td)) rows))
This is first row
This is second row
This is third row
This is fourth row
Table 18.3    In the table expression we map - at the outer level - a composition of tr and a td-mapper. The td-mapper is made by (gmap td).

Recall that we already have discussed the ad hoc currying, which is involved in gmap, cf. the discussion in Section 17.4.

The last example illustrates that (gmap td) is a useful building block, which can be composed with other functions.

The last example depends on the fact that the HTML mirror functions accept lists of elements and attributes.

You should consult Chapter 27 to learn about the exact parameter passing rules of the HTML mirror functions in LAML.

 

18.5.  HTML element modifications
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

It is often useful in some context to bind an attribute of a HTML mirror function (or a number of attributes) to some fixed value(s). This can be done by the higher-order function modify-element, which we discuss below.

The idea behind the function modify-element is to perform an a priori binding of some attributes and some of the contents of a mirror function.

The function modify-element is simple. First notice that it accepts a function, namely the element parameter. It also returns a function; In effect, it returns element with attributes-and-contents appended to the parameters of the modified element. As another possibility, we could have prepended it.

1
2
3
4
(define (modify-element element . attributes-and-contents)
  (lambda parameters 
   (apply element 
    (append parameters attributes-and-contents))))
Program 18.5    The function modify-element.

In the table below we illustrate three examples where td, ol, and ul are modified with a priori bindings of selected attributes.

Expression

Value

(define td1 
 (modify-element td 
  'bgcolor (rgb-color-list red)
  'width 50))

(table 'border 5 
  (map (compose tr (gmap td1)) rows))
This is first row
This is second row
This is third row
This is fourth row
(define ol1 
  (modify-element ol 'type "A"))

(ol1 
 (map
   (compose li as-string)
   (number-interval 1 10)))
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
(define ul1 
  (modify-element ul 'type "square"))

(ul1 
 (map
  (compose li as-string)
  (number-interval 1 10)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Table 18.4    Examples of element modification using the function modify-element.

LAML supports two related, but more advanced functions called xml-in-laml-parametrization and xml-in-laml-abstraction. The first of these is intended to transform an 'old style function' to a function with XML-in-LAML parameter conventions, as explained in Chapter 27. The second function is useful to generate functions with XML-in-LAML parameter conventions in general.

 

18.6.  The function simple-html-table
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

We will now show show an implementation of the function simple-html-table

In an earlier exercise - 'restructuring of lists' - we have used the function simple-html-table

We will now show how it can be implemented

1
2
3
4
5
6
7
8
9
10
(define simple-html-table 
 (lambda (column-widht list-of-rows)
  (let ((gmap (curry-generalized map))
        (td-width 
          (modify-element td 'width
                          (as-string column-widht))))
    (table 
      'border 1
      (tbody 
       (gmap (compose tr (gmap td-width)) list-of-rows))))))
Program 18.6    The function simple-html-table.

 

18.7.  The XHTML mirror in LAML
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

In order to illustrate the data, on which the HTML mirrors in LAML rely, the web edition of the material includes a huge table with the content model and attribute details of each of the 77 XHTML1.0 strict elements.

LAML supports an exact mirror of the 77 XHTML1.0 strict elements as well as the other XHTML variants

The LAML HTML mirror libraries are based on a parsed representation of the HTML DTD (Document Type Definition). The table below is automatically generated from the same data structure.

Element

Content model

Attributes

html (sequence-with-optionals "head" "body")
langdir
xml:langxmlns
head "((script|style|meta|link|object)*, ((title, (script|style|meta|link|object)*, (base, (script|style|meta|link|object)*)?) | (base, (script|style|meta|link|object)*, (title, (script|style|meta|link|object)*))))"
langdir
xml:langprofile
title pcdata-checker
langdir
xml:lang
base "EMPTY"
href
meta "EMPTY"
langname
xml:langcontent
dirscheme
http-equiv
link "EMPTY"
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langcharset
dirhref
onclickhreflang
ondblclicktype
onmousedownrel
onmouseuprev
onmouseovermedia
style pcdata-checker
langmedia
xml:langtitle
dirxml:space
type
script pcdata-checker
charsetdefer
typexml:space
src
noscript (zero-or-more "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
body (zero-or-more "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "ins" "del" "script" "noscript")
idonmouseup
classonmouseover
styleonmousemove
titleonmouseout
langonkeypress
xml:langonkeydown
dironkeyup
onclickonload
ondblclickonunload
onmousedown
div (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
p (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h1 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h2 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h3 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h4 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h5 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
h6 (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
ul (one-or-more "li")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
ol (one-or-more "li")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
li (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
dl (one-or-more "dt" "dd")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
dt (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
dd (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
address (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
hr "EMPTY"
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
pre (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "map" "tt" "i" "b" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclickxml:space
blockquote (zero-or-more "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclickcite
ins (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmouseup
classonmouseover
styleonmousemove
titleonmouseout
langonkeypress
xml:langonkeydown
dironkeyup
onclickcite
ondblclickdatetime
onmousedown
del (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmouseup
classonmouseover
styleonmousemove
titleonmouseout
langonkeypress
xml:langonkeydown
dironkeyup
onclickcite
ondblclickdatetime
onmousedown
a (zero-or-more "#PCDATA" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonkeydown
classonkeyup
stylecharset
titletype
langname
xml:langhref
dirhreflang
onclickrel
ondblclickrev
onmousedownaccesskey
onmouseupshape
onmouseovercoords
onmousemovetabindex
onmouseoutonfocus
onkeypressonblur
span (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
bdo (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
onclickonkeyup
ondblclicklang
onmousedownxml:lang
onmouseupdir
onmouseover
br "EMPTY"
idstyle
classtitle
em (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
strong (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
dfn (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
code (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
samp (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
kbd (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
var (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
cite (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
abbr (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
acronym (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
q (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclickcite
sub (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
sup (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
tt (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
i (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
b (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
big (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
small (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
object (zero-or-more "#PCDATA" "param" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonkeydown
classonkeyup
styledeclare
titleclassid
langcodebase
xml:langdata
dirtype
onclickcodetype
ondblclickarchive
onmousedownstandby
onmouseupheight
onmouseoverwidth
onmousemoveusemap
onmouseoutname
onkeypresstabindex
param "EMPTY"
idvaluetype
nametype
value
img "EMPTY"
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langsrc
diralt
onclicklongdesc
ondblclickheight
onmousedownwidth
onmouseupusemap
onmouseoverismap
map "((p | h1|h2|h3|h4|h5|h6 | div | ul | ol | dl | pre | hr | blockquote | address | fieldset | table | form | ins | del | script | noscript)+ | area+)"
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclickid
onmousedownclass
onmouseupstyle
onmouseovertitle
onmousemovename
area "EMPTY"
idonmouseout
classonkeypress
styleonkeydown
titleonkeyup
langshape
xml:langcoords
dirhref
onclicknohref
ondblclickalt
onmousedowntabindex
onmouseupaccesskey
onmouseoveronfocus
onmousemoveonblur
form (zero-or-more "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "ins" "del" "script" "noscript")
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langaction
dirmethod
onclickenctype
ondblclickonsubmit
onmousedownonreset
onmouseupaccept
onmouseoveraccept-charset
label (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
dirfor
onclickaccesskey
ondblclickonfocus
onmousedownonblur
onmouseup
input "EMPTY"
idname
classvalue
stylechecked
titledisabled
langreadonly
xml:langsize
dirmaxlength
onclicksrc
ondblclickalt
onmousedownusemap
onmouseuptabindex
onmouseoveraccesskey
onmousemoveonfocus
onmouseoutonblur
onkeypressonselect
onkeydownonchange
onkeyupaccept
type
select (one-or-more "optgroup" "option")
idonmouseout
classonkeypress
styleonkeydown
titleonkeyup
langname
xml:langsize
dirmultiple
onclickdisabled
ondblclicktabindex
onmousedownonfocus
onmouseuponblur
onmouseoveronchange
onmousemove
optgroup (one-or-more "option")
idonmouseup
classonmouseover
styleonmousemove
titleonmouseout
langonkeypress
xml:langonkeydown
dironkeyup
onclickdisabled
ondblclicklabel
onmousedown
option pcdata-checker
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
dirselected
onclickdisabled
ondblclicklabel
onmousedownvalue
onmouseup
textarea pcdata-checker
idonkeypress
classonkeydown
styleonkeyup
titlename
langrows
xml:langcols
dirdisabled
onclickreadonly
ondblclicktabindex
onmousedownaccesskey
onmouseuponfocus
onmouseoveronblur
onmousemoveonselect
onmouseoutonchange
fieldset (zero-or-more "#PCDATA" "legend" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
legend (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclickaccesskey
button (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "table" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "ins" "del" "script" "noscript")
idonmouseout
classonkeypress
styleonkeydown
titleonkeyup
langname
xml:langvalue
dirtype
onclickdisabled
ondblclicktabindex
onmousedownaccesskey
onmouseuponfocus
onmouseoveronblur
onmousemove
table "(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))"
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langsummary
dirwidth
onclickborder
ondblclickframe
onmousedownrules
onmouseupcellspacing
onmouseovercellpadding
caption (zero-or-more "#PCDATA" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonmousedown
classonmouseup
styleonmouseover
titleonmousemove
langonmouseout
xml:langonkeypress
dironkeydown
onclickonkeyup
ondblclick
thead (one-or-more "tr")
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langspan
dirwidth
onclickalign
ondblclickchar
onmousedowncharoff
onmouseupvalign
onmouseover
tfoot (one-or-more "tr")
idonmousemove
classonmouseout
styleonkeypress
titleonkeydown
langonkeyup
xml:langspan
dirwidth
onclickalign
ondblclickchar
onmousedowncharoff
onmouseupvalign
onmouseover
tbody (one-or-more "tr")
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
diralign
onclickchar
ondblclickcharoff
onmousedownvalign
onmouseup
colgroup (zero-or-more "col")
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
diralign
onclickchar
ondblclickcharoff
onmousedownvalign
onmouseup
col "EMPTY"
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
diralign
onclickchar
ondblclickcharoff
onmousedownvalign
onmouseup
tr (one-or-more "th" "td")
idonmouseover
classonmousemove
styleonmouseout
titleonkeypress
langonkeydown
xml:langonkeyup
diralign
onclickchar
ondblclickcharoff
onmousedownvalign
onmouseup
th (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonkeypress
classonkeydown
styleonkeyup
titleabbr
langaxis
xml:langheaders
dirscope
onclickrowspan
ondblclickcolspan
onmousedownalign
onmouseupchar
onmouseovercharoff
onmousemovevalign
onmouseout
td (zero-or-more "#PCDATA" "p" "h1" "h2" "h3" "h4" "h5" "h6" "div" "ul" "ol" "dl" "pre" "hr" "blockquote" "address" "fieldset" "table" "form" "a" "br" "span" "bdo" "object" "img" "map" "tt" "i" "b" "big" "small" "em" "strong" "dfn" "code" "q" "sub" "sup" "samp" "kbd" "var" "cite" "abbr" "acronym" "input" "select" "textarea" "label" "button" "ins" "del" "script" "noscript")
idonkeypress
classonkeydown
styleonkeyup
titleabbr
langaxis
xml:langheaders
dirscope
onclickrowspan
ondblclickcolspan
onmousedownalign
onmouseupchar
onmouseovercharoff
onmousemovevalign
onmouseout
Table 18.5    HTML elements, their status, and their attributes in the XHTML1.0 strict Scheme mirror. The Content model information is - for most elements - shown as predicates which validate the individual elements. An EMPTY indication tells that there is no content model to validate, because the element is a terminal one (no constituent elements). For a few elements a non-trivial string is given as content model. These elements calls for specialized validation predicates, which have be hand written, from case to case.

 

18.8.  Generation of a leq predicate from enumeration
Contents   Up Previous Next   Slide    Subject index Program index Exercise index 

As the last example related to higher-order functions we show the function generate-leq, see Program 18.7.

The idea is to generate a boolean 'less than or equal' (leq) function based on an explicit enumeration order, which is given as input to the function generate-leq. A number of technicalities are involved. You should read the details in Program 18.7 to grasp these details.

In some contexts we wish to specify a number of clauses in an arbitrary order

For presentational clarity, we often want to ensure that the clauses are presented in a particular order

Here we want to generate a leq predicate from an enumeration of the desired order

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;; Generate a less than or equal predicate from the
;; enumeration-order. If p is the generated predicate,
;; (p x y) is true if and only if (selector x) comes before
;; (or at the same position) as (selector y) in the
;; enumeration-order. Thus, (selector x) is assumed to give a
;; value in enumeration-order. Comparison with elements in the
;; enumeration-list is done with eq?
(define (generate-leq enumeration-order selector)
  (lambda (x y)
     ; x and y supposed to be elements in enumeration order
     (let ((x-index (list-index (selector x) enumeration-order))
           (y-index (list-index (selector y) enumeration-order)))
       (<= x-index y-index))))

; A helping function of generate-leq.
; Return the position of e in lst. First is 1
; compare with eq?
; if e is not member of lst return (+ 1 (length lst))
(define (list-index e lst)
 (cond ((null? lst) 1)
       ((eq? (car lst) e) 1)
       (else (+ 1 (list-index e (cdr lst))))))
Program 18.7    The functions generate-leq and the helping function list-index .

The table below shows a very simple example, in which we use simple-leq?, which is generated by the higher-order function generate-leq from Program 18.7.

Expression

Value

(define simple-leq? 
  (generate-leq '(z a c b y x) id-1))

(sort-list '(a x y z c c b a) simple-leq?)
(z a a c c b y x)
Table 18.6    A simple example of an application of generate-leq.

The fragment in Program 18.8 gives a more realistic example of the use of generated 'less than or equal' functions. In Program 18.9 we show how the desired sorting of manual-page subelements is achieved.

1
2
3
4
5
6
7
8
9
(manual-page 
 (form '(show-table rows))
 (title "show-table")
 (description "Presents the table, in terms of rows")
 (parameter "row" "a list of elements")
 (pre-condition "Must be placed before the begin-notes clause")
 (misc "Internally, sets the variable lecture-id")
 (result "returns an HTML string")
)
Program 18.8    A hypothetical manual page clause.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(define (syntactic-form name)
  (lambda subclauses (cons name subclauses)))

(define form (syntactic-form 'form))
(define title (syntactic-form 'title))
(define description (syntactic-form 'description))
(define parameter (syntactic-form 'parameter))
(define pre-condition (syntactic-form 'pre-condition))
(define misc (syntactic-form 'misc))
(define result (syntactic-form 'result))

(define (manual-page . clauses)
 (let ((clause-leq? 
         (generate-leq
           '(title form description 
             pre-condition result misc)
           first))
      )
  (let ((sorted-clauses (sort-list clauses clause-leq?)))
    (present-clauses sorted-clauses))))
Program 18.9    An application of generate-leq which sorts the manual clauses.

Generated: Tuesday July 2, 2013, 09:15:38
Theme index -- Keyboard shortcut: 'u'  Previous theme in this lecture -- Keyboard shortcut: 'p'  Next slide in this lecture -- Keyboard shortcut: 'n'