1. datatype
    1. Description
    2. Author
    3. Repository
    4. Requirements
    5. Documentation
      1. Example
    6. Changelog
    7. License

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

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/datatype

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Requirements

None

Documentation

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

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.

[syntax] (cases TYPENAME EXP CLAUSE ...)

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.