This module implements the cell datatype, where a cell is simply a closure over some state, which accepts either zero or one argument. Without an argument it returns its state, and with argument it changes its state according to this argument.
Usually, cells are named with a trailing % to give a hint to this nature.
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)
Creates and initializes a cell.
cell?[procedure] (cell? xpr)
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 cell%)
accessor: returns the content of cell%.
cell-set![procedure] (cell-set! cell% val)
mutator: changes the content of cell%
(use cells) (define cell% (cell 5)) (cell? cell%) ; -> #t (cell? 5) ; -> #f ((cell-of? number?) cell%) ; -> #t ((cell-of? list?) cell%) ; -> #f (cell%) ; -> 5 (cell-ref cell%) ; -> 5 (cell% 50) (cell%) ; -> 50 (cell-set! cell% 500) (cell-ref cell%) ; -> 500 (cell%) ; -> 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 '())) ; or '#,(% ())) (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)
Mar 15, 2014
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.
- compound-test added
- added cells, cell-ref and cell-set!
- initial import