Note: This is taken from the Chicken Wiki, where a more recent version could be available.
A facility for creating and using variant records, as described in the book "Essentials of Programming Languages" by Friedman, Wand and Haynes.
<macro>(define-datatype TYPENAME [PREDICATE] VARIANT ...)</macro>
Defines a record type named TYPENAME, where VARIANT ... defines one or more constructors for instances of this type. VARIANT should be of the form
VARIANT = (CONSTRUCTOR (FIELDNAME FIELDPRED) ...)
CONSTRUCTOR is the name of a constructor procedure that will be defined with as many arguments as fields are specified. (FIELDNAME FIELDPRED) ... specify the name for each field and a procedure of one argment that should return a true value for legal field values.
The optional PREDICATE should be the name of a procedure that will be defined and which returns #t when applied to an instance of this variant record.
<macro>(cases TYPENAME EXP CLAUSE ...)</macro>
A facility for matching and deconstructing the instance EXP of the variant record with the name TYPENAME. Each CLAUSE specifies a constructor with field-names and a body to execute when the constructor matches the record instance:
CLAUSE = (CONSTRUCTOR (FIELDNAME ...) BODY ...) | (else BODY ...)
<example> <expr> ; A simple binary tree with numbers:
(define-datatype tree tree?
(leaf (n number?)) (branch (left tree?) (right tree?)) )
(define t (branch (branch (leaf 33) (leaf 44)) (leaf 55)))
(define (listify t)
(cases tree t (leaf (n) n) (branch (left right) (cons (listify left) (listify right))) ) )
(listify t) </expr> <result>((33 . 44) . 55)</result> </example>
Copyright (c) 2003, Felix L. Winkelmann All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.