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

cells

This module implements the cell datatype as a record. It can be converted to a function, closed over some state. If this function is called without arguments, it returns the state, with one argument, it changes state.

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.

cell

[procedure] (cell arg)

constructor.

Creates and initializes a cell.

cell?

[procedure] (cell? xpr)

type predicate.

Does xpr evaluate to a cell?

cell-of?

[procedure] (cell-of? type?)

returns a predicate, which checks, if its only argument is a cell which stores an item of type?

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.

cell->closure

[procedure] (cell->closure cl)

transforms its cell argument into a closure

Usage

(use cells)

Examples

(use 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
(define cc (cell->closure cl))
(cc) ; -> 500
(cc 5)
(cc) ; -> 5

;;; 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
(define stack% (cell->closure stack))
(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

Jul 30, 2014

Author

Juergen Lorenz

License

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

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