chickadee » datatype

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

datatype

Description

A facility for creating and using variant records, as described in the book "Essentials of Programming Languages" by Friedman, Wand and Haynes.

Author

felix winkelmann

Requirements

None

Documentation

(define-datatype TYPENAME [PREDICATE] VARIANT ...)syntax

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.

(cases TYPENAME EXP CLAUSE ...)syntax

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

; A simple binary tree with numbers:

(require-for-syntax 'datatype)

(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)
 => ((33 . 44) . 55)

Changelog

License

 Copyright (c) 2003-2010, 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.

Contents »