Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution 3
Table Column Exercise


(load (string-append laml-dir "laml.scm"))
(laml-style "simple-xhtml1.0-transitional-validating")

(set-xml-accept-only-string-valued-attributes-in 'xhtml10-transitional #f)
(set-xml-accept-extended-contents-in 'xhtml10-transitional #t)

; The table columns:
(define col1 (list 1 5 #f 9 17))
(define col2 (list 15 #f 0 6))
(define col3 (list 8 #f 11))

; For a table with rows (a list of rows) of the three columns
(define (form-full-table-with-rows col1 col2 col3)
  (let ((number-of-rows (max (length col1) (length col2) (length col3))))
    (map (lambda (row-number) 
            (get-row-number row-number col1 col2 col3))
         (number-interval 1 number-of-rows))))

; Return row number n from the three columns
(define (get-row-number n col1 col2 col3)
  (list
    (get-element col1 n) (get-element col2 n) (get-element col3 n)))

; Get element number n of list. Return #f if the element is not there.
; The first element of a list counts as number 1
(define (get-element lst n)
  (let ((lgt (length lst)))
    (if (<= n lgt)
        (list-ref lst (- n 1))
        #f)))

(define (present-table col1 col2 col3)
  (table 'border 1
    (map (compose tr td-mapper)
         (add-rows (form-full-table-with-rows col1 col2 col3)))))

(define (td-mapper lst)
  (map td lst))

; Add the elements in all rows of list-of-rows. Be careful to handle #f as zero.
(define (add-rows list-of-rows)
  (map
    (lambda (row)
      (append row (list (sum-list-special row))))
    list-of-rows))       

; Add the elements in lst, handling #f as zero.
(define (sum-list-special lst)
  (accumulate-right plus-special 0 lst))

; Specialized plus.
(define (plus-special x y)
  (cond ((and (and (boolean? x) (not x)) (and (boolean? y) (not y))) 0)
        ((and (boolean? x) (not x)) y)
        ((and (boolean? y) (not y)) x)
        (else (+ x y))))


; Web page generation stuff:

(define current-xml-language 'xhtml10-transitional)
(define laml-generation-meta (meta 'name "Generator" 'content "LAML"))
(define meta-props (list 'http-equiv "Content-Type" 'content "text/html; charset=iso-8859-1"))
(define html-props (list 'xmlns "http://www.w3.org/1999/xhtml"))

; Insert the LAML template "Processing Options" here
; if you need variations in the LAML processing

(write-html '(raw prolog)
 (html html-props
  (head 
   (meta meta-props) laml-generation-meta
   (title "Table Columns"))
  (body 
    (present-table col1 col2 col3))
 )
)


(end-laml)