Changeset 31168 in project


Ignore:
Timestamp:
07/30/14 17:15:01 (6 years ago)
Author:
juergen
Message:

cells version 1.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/cells

    r30569 r31168  
    44== cells
    55
    6 This module implements the cell datatype, where a cell is simply a
    7 closure over some state, which accepts either zero or one argument.
    8 Without an argument it returns its state, and with argument it changes
    9 its state according to this argument.
    10 
    11 Usually, cells are named with a trailing % to give a hint to this
    12 nature.
     6This module implements the cell datatype as a record. It can be
     7converted to a function, closed over some state. If this function is
     8called without argumentes, it returns the state, with one argument, it
     9changes state.
    1310
    1411=== Programming interface
     
    4643==== cell-ref
    4744
    48 <procedure>(cell-ref cell%)</procedure>
     45<procedure>(cell-ref cl)</procedure>
    4946
    50 accessor: returns the content of cell%.
     47accessor: returns the content of cell cl.
    5148
    5249==== cell-set!
    5350
    54 <procedure>(cell-set! cell% val)</procedure>
     51<procedure>(cell-set! cl val)</procedure>
    5552
    56 mutator: changes the content of cell%
     53mutator: changes the content of cell cl to val.
     54
     55==== cell->closure
     56
     57<procedure>(cell->closure cl)</procedure>
     58
     59transforms its cell argument into a closure
    5760
    5861=== Usage
     
    6770(use cells)
    6871
    69 (define cell% (cell 5))
    70 (cell? cell%) ; -> #t
     72(define cl (cell 5))
     73(cell? cl) ; -> #t
    7174(cell? 5) ; -> #f
    72 ((cell-of? number?) cell%) ; -> #t
    73 ((cell-of? list?) cell%) ; -> #f
    74 (cell%) ; -> 5
    75 (cell-ref cell%) ; -> 5
    76 (cell% 50)
    77 (cell%) ; -> 50
    78 (cell-set! cell% 500)
    79 (cell-ref cell%) ; -> 500
    80 (cell%) ; -> 500
     75((cell-of? number?) cl) ; -> #t
     76((cell-of? list?) cl) ; -> #f
     77(cell-ref cl) ; -> 5
     78(cell-set! cl 50)
     79(cell-ref cl) ; -> 50
     80(cell-set! cl 500)
     81(cell-ref cl) ; -> 500
     82(define cc (cell->closure cl))
     83(cc) ; -> 500
     84(cc 5)
     85(cc) ; -> 5
    8186
    8287;;; implementing a stack without any error checking
     
    8893  (st% (cdr (st%))))
    8994
    90 (define stack% (cell '())) ; or '#,(% ()))
    91 (cell? stack%) ; -> #t
    92 ((cell-of? list?) stack%) ; -> #t
    93 ((cell-of? number?) stack%) ; -> #f
     95(define stack (cell '()))
     96(cell? stack) ; -> #t
     97((cell-of? list?) stack) ; -> #t
     98((cell-of? number?) stack) ; -> #f
     99(define stack% (cell->closure stack))
    94100(null? (stack%)) ; -> #t
    95101(push stack% 5)
     
    110116== Last update
    111117
    112 Mar 15, 2014
     118Jul 30, 2014
    113119
    114120== Author
     
    149155== Version History
    150156
     157; 1.2 : cells as records, cell->closure added
    151158; 1.1.1 : compound-test added
    152159; 1.1  : added cells, cell-ref and cell-set!
Note: See TracChangeset for help on using the changeset viewer.