You are looking at historical revision 25401 of this page. It may differ significantly from its current revision.

## tuples

Tuples are a datatype which has much in common with vectors. Namely they store a finite number of items with random access and make them available via (tuple-ref tup n). But contrary to vectors, the items can only by accessed but not assigned to. If you want to change a particular item, you must package it into a box or a single, which is a special tuple storing exactly one item, which can be changed via single-state! without changing the identity of the single object itself.

singles are the only tuples, which can assign to their content. Other special tuples are empty, couple and triple. Note, that couples can be used instead of pairs to implement immutable lists.

## Documentation

In this implementation tuples are implemented in the Design by Contract style, i.e. using the contracts module. A corollary of this is, that the documentation is included in the module. By convention, there is a routine with the module's name, tuples, which when called with no argument

(tuples)

shows the exported contract-checked symbols, and called with one of its symbols, e.g.

(tuples 'tuple->list)

shows the documentation of this symbol in all it's glory, i.e. together with call-structure, docstring, assumptions and propositions.

Note that most contract-checked routines have brethren whith identical signature which are not contract-checked and are not exported. They are prefixed with an % sign and used internally, especially for contract-checking, thus avoiding double-checking.

Another way to get the complete documentation is to use print-doclist from the contracts module. Issuing

(import tuples (only contracts print-doclist)) (with-output-to-file "tuples.docu" (lambda () (print-doclist)))

you'll get a file tuples.docu with the following content

triple-right ------------ (triple-right tup) returns the right item of a triple

triple-middle ------------- (triple-middle tup) returns the middle item of a triple

triple-left ----------- (triple-left tup) returns the left item of a triple

triple ------ (triple x y z) constructor for a tuple storing two items

triple? ------- (triple? x) tests for a tuple storing two items

couple-right ------------ (couple-right tup) returns the right item of a couple

couple-left ----------- (couple-left tup) returns the left item of a couple

couple ------ (couple x y) constructor for a tuple storing two items

couple? ------- (couple? x) tests for a tuple storing two items

single-state! ------------- (single-state! sg arg) replaces state of sg with arg (domain (%single? sg) (true? arg)) (effect (state (%tuple-state sg) arg))

single-state ------------ (single-state sg) returns the state of the single object sg (domain (%single? sg))

single ------ (single xpr) package xpr into a box so that it can be modified (domain (true? xpr)) (range (%single? result))

single? ------- (single? xpr) check, if xpr evaluates to a single

empty ----- (empty) constructor for an empty tuple

empty? ------ (empty? x) tests for an empty tuple

tuple-for-each -------------- (tuple-for-each proc tup) apply a one parameter procedure to each item of tup (domain (%tuple? tup) (procedure? proc) a one parameter procedure)

tuple->list ----------- (tuple->list tup) transforms a tuple into a list (domain (%tuple? tup)) (range (list? result))

tuple-find ---------- (tuple-find tup item compare?) checks by comparing with compare? if item is contained in tup (domain (%tuple? tup) (procedure? compare?) a two parameter predicate) (range (or (not result) (and (cardinal? result) (< result (%tuple-length tup)))))

tuple-right ----------- (tuple-right tup) returns the rightmost item of tup (domain (%tuple? tup) (>= (%tuple-length tup) 2))

tuple-left ---------- (tuple-left tup) returns the leftmost item of tup (domain (%tuple? tup) (positive? (%tuple-length tup)))

tuple-ref --------- (tuple-ref tup n) returns the n'th item of tup, counting from zero (domain (%tuple? tup) (cardinal? n) (< n (%tuple-length tup)))

tuple-length ------------ (tuple-length tup) returns the number of tuple items (domain (%tuple? tup)) (range (cardinal? result))

tuple-copy ---------- (tuple-copy tup . interval) constructing a subtuple with tup data from interval (domain (%tuple? tup) (<= (length interval) 2) ((list-of? cardinal?) interval) (apply <= (append interval (list (%tuple-length tup))))) (range (%tuple? result))

tuple-append ------------ (tuple-append . tups) constructs a new tuple by concatenating several others (domain ((list-of? tuple?) tups)) (range (%tuple? result))

tuple-map --------- (tuple-map fn tup) constructs a new tuple by mapping each item of tup with function fn (domain (%tuple? tup) (procedure? fn) a one parameter function) (range (%tuple? result))

list->tuple ----------- (list->tuple lst) transforms a list into a tuple (domain (list? lst)) (range (%tuple? result))

tuple ----- (tuple . args) tuple constructor (domain (true? args)) (range (%tuple? result))

tuple-of? --------- (tuple-of? ok?) checks, if each tuple item satisfies predicate ok? (domain (procedure? ok?) ok? is a one parameter predicate) (range (procedure? result) result is a one parameter predicate)

tuple? ------ (tuple? xpr) checks if xpr evaluates to a tuple

## Requirements

## Last update

Oct 20, 2011

## Author

## License

Copyright (c) 2011, Juergen Lorenz 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.

## Version History

- 0.6
- contracts rewritten
- 0.5
- code maintenance
- 0.4
- added copyright notice
- 0.3
- updated for using contracts 1.0
- 0.2
- added tuple-copy, tuple-left, tuple-right
- 0.1
- initial import