1. cells
    1. Programming interface
      1. cells
      2. make-cell-of
      3. cell-of?
      4. cell-empty?
      5. cell-prune!
      6. make-cell
      7. cell
      8. cell?
      9. cell-ref
      10. cell-set!
    2. Usage
    3. Examples
    4. Requirements
  2. Last update
  3. Author
  4. License
  5. Version History

cells

This module implements the cell datatype as a closure. If this closure is called without argument, it returns the state, with one argument, it changes state. Cells can now be type-checked. For convenience, cell-ref and cell-set! syntax is provided as well.

Programming interface

cells

[procedure] (cells [sym])

documentation procedure: Without an argument shows the list of exported symbols, with a symbol argument shows the signature of this symbol.

make-cell-of

[procedure] (make-cell-of . predicates)

constructor. creates an empty typed cell, whose state must pass all predicate checks.

cell-of?

[procedure] (cell-of? . predicates)

creates a predicate, which tests if it is a cell, whose state passes the predicate checks.

cell-empty?

[procedure] (cell-empty? xpr)

checks if xpr evaluates to an empty cell.

cell-prune!

[procedure] (cell-prune! c%)

empties its cell argument.

make-cell

[procedure] (make-cell)

creates an empty untyped cell.

cell

[procedure] (cell arg)

Creates and initializes an untyped cell.

cell?

[procedure] (cell? xpr)

checks, if xpr evaluates to an untyped cell.

cell-ref

[procedure] (cell-ref cl)

accessor: returns the content of cell cl.

cell-set!

[procedure] (cell-set! cl val)

mutator: changes the content of cell cl to val.

Usage

(use cells)

Examples

(use cells)

;;; typed cells
(define c% (make-cell-of number? positive?))
((cell-of? number? positive?) c%) ; -> #t
(cell-empty? c%) ; -> #t
(c% 5)
(cell-empty? c%) ; -> #f
(c%) ; -> 5
((cell-of? number? positive?) c%) ; -> #t
(c%) ; -> 5
((cell-of? number? zero?) c%) ; -> #f
((cell-of? number?) c%) ; -> #t
((cell-of?) c%) ; -> #t
(cell-prune! c%)
(cell-empty? c%) ; -> #t
(condition-case (c% -5)
  ((exn) #f)) ; -> #f
(define d% (make-cell-of number?))
(d% -5)
(d%) ; -> 5
((cell-of? number? positive?) d%) ; -> #f
((cell-of? number?) d%) ; -> #t

;;; untyped cells
(define cl (cell 5))
(cell? cl) ; -> #t
(cell? 5) ; -> #f
((cell-of? number?) cl) ; -> #t
((cell-of? list?) cl) ; -> #f
(cell-ref cl) ; -> 5
(cell-set! cl 50)
(cell-ref cl) ; -> 50
(cell-set! cl 500)
(cell-ref cl) ; -> 500

;;; implementing a stack without any error checking
(define (push st% val)
  (st% (cons val (st%))))
(define (top st%)
  (car (st%)))
(define (pop st%)
  (st% (cdr (st%))))

(define stack (cell '()))
(cell? stack) ; -> #t
((cell-of? list?) stack) ; -> #t
((cell-of? number?) stack) ; -> #f
(null? (stack)) ; -> #t
(push stack 5)
(push stack 50)
(push stack 500)
(top stack) ; -> 500
(pop stack)
(top stack) ; -> 50 
(pop stack)
(top stack) ; -> 5)

Requirements

None

Last update

Jun 21, 2015

Author

Juergen Lorenz

License

Copyright (c) 2011-2015, 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

2.1
scope bug fixed
2.0
type-checked cells added, cells implemented as closures, hence cell->closure removed
1.2
cells as records, cell->closure added
1.1.1
compound-test added
1.1
added cells, cell-ref and cell-set!
1.0
initial import