| [ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Named-Catamorphisms
It is also possible to explicitly name the operator in the “cata” position.
Where ,[id*] recurs to the top of the current sxml-match,
,[cata -> id*] recurs to cata. cata must evaluate to a
procedure which takes one argument, and returns as many values as there are
identifiers following ->.
Named catamorphism patterns allow processing to be split into multiple, mutually recursive procedures. This is illustrated in the example below: a transformation that formats a “TV Guide” into HTML.
(define (tv-guide->html g)
(define (cast-list cl)
(sxml-match cl
[(CastList (CastMember (Character (Name ,ch)) (Actor (Name ,a))) ...)
`(div (ul (li ,ch ": " ,a) ...))]))
(define (prog p)
(sxml-match p
[(Program (Start ,start-time) (Duration ,dur) (Series ,series-title)
(Description ,desc ...))
`(div (p ,start-time
(br) ,series-title
(br) ,desc ...))]
[(Program (Start ,start-time) (Duration ,dur) (Series ,series-title)
(Description ,desc ...)
,[cast-list -> cl])
`(div (p ,start-time
(br) ,series-title
(br) ,desc ...)
,cl)]))
(sxml-match g
[(TVGuide (@ (start ,start-date)
(end ,end-date))
(Channel (Name ,nm) ,[prog -> p] ...) ...)
`(html (head (title "TV Guide"))
(body (h1 "TV Guide")
(div (h2 ,nm) ,p ...) ...))]))
This document was generated on April 20, 2013 using texi2html 5.0.
