You are looking at historical revision 23421 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 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.

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

couple ------ (couple x y) "constructor for a tuple storing two items" couple-left ----------- (couple-left tup) "returns the left item of a couple" couple-right ------------ (couple-right tup) "returns the right item of a couple" couple? ------- (couple? x) "tests for a tuple storing two items" empty ----- (empty) "constructor for an empty tuple" empty? ------ (empty? x) "tests for an empty tuple" list->tuple ----------- (list->tuple lst) "transforms a list into a tuple" (domain: (list? lst)) (range: (tuple? result)) single ------ (single xpr) "package xpr into a box so that it can be modified" (domain: (true? xpr)) (range: (procedure? result)) single-state ------------ (single-state sg) "returns the state of a single sg" single-state! ------------- (single-state! sg arg) "modifies the state of a single sg to new" single-state% ------------- (single-state% sg . args) "replaces state of sg with (car args), returns its state if (null? args)" (domain: (single? sg) (<= (length args) 1)) (state: state) (invariant: (true? state)) (state: (old new)) (effect: (equal? new (car args))) single? ------- (single? xpr) "check, if xpr evaluates to a single" triple ------ (triple x y z) "constructor for a tuple storing two items" triple-left ----------- (triple-left tup) "returns the left item of a triple" triple-middle ------------- (triple-middle tup) "returns the middle item of a triple" triple-right ------------ (triple-right tup) "returns the right item of a triple" triple? ------- (triple? x) "tests for a tuple storing two items" tuple ----- (tuple . args) "tuple constructor" (domain: (true? args)) (range: (tuple? result) "a routine which chooses from a selector") tuple->list ----------- (tuple->list tup) "transforms a tuple into a list" (domain: (tuple? tup)) (range: (list? result)) tuple-append ------------ (tuple-append . tups) "constructs a new tuple by concatenating several others" (domain: ((list-of? tuple?) tups)) (range: (tuple? result)) tuple-copy ---------- (tuple-copy tup . range) "constructs a subtuple with tup data from range (if given, otherwise canonical choice)" (domain: (tuple? tup) (<= (length range) 2) ((list-of? cardinal?) range) (apply <= (append range (list (tuple-length tup))))) (range: (tuple? 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-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-left ---------- (tuple-left tup) "returns the leftmost item of tup" (domain: (tuple? tup) (positive? (tuple-length tup))) tuple-length ------------ (tuple-length tup) "returns the number of tuple items" (domain: (tuple? tup)) (range: (cardinal? 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)) 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-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-right ----------- (tuple-right tup) "returns the rightmost item of tup" (domain: (tuple? tup) (>= (tuple-length tup) 2)) tuple? ------ (tuple? xpr) "checks if xpr evaluates to a tuple"

## Requirements

## 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.1
- initial import
- 0.2
- added tuple-copy, tuple-left, tuple-right