Changeset 31265 in project


Ignore:
Timestamp:
08/22/14 13:05:08 (5 years ago)
Author:
juergen
Message:

typed-lists functor split in two

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/typed-lists

    r31239 r31265  
    66
    77Scheme lists are mutable and untyped. The list types produced with
    8 this functor module are immutable and typed.
     8this library are immutable and typed.
    99
    1010Concerning mutability, Scheme's authors, G. J. Sussman and G. L. Steele jr.
     
    2929some symbol depending on the use of the macro. Well, that worked, but it
    3030was rather ugly. There is a much more elegant and flexible solution, which
    31 depends on functors, which are unfortunately rarely known by programmers.
     31depends on functors, which are unfortunately rarely known by Chicken
     32programmers.
    3233
    3334Recall, that functors are to Chicken modules what functions are to
    34 Chicken values. In other words, functors are defined with abstract module
     35Scheme values. In other words, functors are defined with abstract module
    3536parameters, which produce modules only when fed with concrete module
    3637arguments. This is exactly what we need. The abstract module parameter
     
    4041one version of the functor value is used.
    4142
    42 Hence, this module implements a functor, typed-lists, depending on a
     43Hence, this library implements a functor, typed-lists, depending on a
    4344module parameter which must supply two exports, named equ? and type?.
    4445The resulting code -- when applied to a concrete module which implements
    45 these two routines -- defines list and set datatypes, to be more
    46 precise, immutable typed lists and immutable typed sets, the latter
    47 being equivalence classes of the former.
     46these two routines -- defines immutable typed lists.
    4847
    4948Note, that these typed lists don't pass the list? predicate. They
     
    5150cases from the datatype module.
    5251
     52As an application of the typed-lists functor, this library defines
     53another functor, typed-sets, which depends on a second module, being
     54generated by the typed-lists functor, which generates immutable typed sets,
     55being implemented as equivalence classes of immutable typed lists.
     56
    5357=== typed-lists
    5458
    55 the functor's name, exporting typed lists and (typed) sets.
    56 
    57 === Usage
     59the first functor's name, generating immutable typed lists when applied
     60to a module which provides the proper type? and equ? symbols.
     61
     62=== typed-sets
     63
     64the second functor's name, generating immutable typed sets when applied
     65to two modules, the first one providing the proper type? and equ?
     66symbols, the second being generated by invoking typed-lists with the
     67first module.
     68
     69=== Usage of typed-lists
    5870
    5971<enscript highlight=scheme>
     
    6274
    6375;; define argument module
    64 (module arg-name (type? equ?)
     76(module vals (type? equ?)
    6577  (import scheme)
    6678  (define type? ...)
     
    6880
    6981;; apply functor
    70 (module val-name = (typed-lists arg-name))
     82(module val-lists = (typed-lists vals))
    7183
    7284;; import the functor
    73 (import val-name)
     85(import val-lists)
    7486
    7587;; now use the generated routines
     
    328340Then you can use bind and friends and freely mix typed lists with other
    329341sequence types.
     342
     343=== Usage of typed-sets
     344
     345<enscript highlight=scheme>
     346(require-library datatype)
     347(import typed-lists typed-sets datatype)
     348
     349;; define first argument module
     350(module vals (type? equ?)
     351  (import scheme)
     352  (define type? ...)
     353  (define equ? ...))
     354
     355;; apply typed-lists functor to provide second argument module
     356(module val-lists = (typed-lists vals))
     357
     358;; apply typed-sets functor
     359(module val-sets = (typed-sets vals val-lists))
     360
     361;; import the modules
     362(import val-lists val-sets)
     363
     364;; now use the generated routines
     365</enscript>
    330366
    331367=== Generated functions of the set datatype
     
    545581;;; number-sets
    546582;;; -----------
     583;; generate module and import it
     584(module sets = (typed-sets nums lists)
     585(import sets)
     586
     587;; apply it
    547588(set=
    548589  (typed-list->set (typed-list 1 2 1 3 2 3))
     
    630671== Last update
    631672
    632 Aug 18, 2014
     673Aug 22, 2014
    633674
    634675== Author
     
    669710== Version History
    670711
     712; 1.3 : functor split in two
    671713; 1.2 : list-cons-sorted added
    672714; 1.1 : list-bind corrected, list-in? added
Note: See TracChangeset for help on using the changeset viewer.