Changeset 31265 in project
 Timestamp:
 08/22/14 13:05:08 (5 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

wiki/eggref/4/typedlists
r31239 r31265 6 6 7 7 Scheme lists are mutable and untyped. The list types produced with 8 this functor moduleare immutable and typed.8 this library are immutable and typed. 9 9 10 10 Concerning mutability, Scheme's authors, G. J. Sussman and G. L. Steele jr. … … 29 29 some symbol depending on the use of the macro. Well, that worked, but it 30 30 was rather ugly. There is a much more elegant and flexible solution, which 31 depends on functors, which are unfortunately rarely known by programmers. 31 depends on functors, which are unfortunately rarely known by Chicken 32 programmers. 32 33 33 34 Recall, that functors are to Chicken modules what functions are to 34 Chickenvalues. In other words, functors are defined with abstract module35 Scheme values. In other words, functors are defined with abstract module 35 36 parameters, which produce modules only when fed with concrete module 36 37 arguments. This is exactly what we need. The abstract module parameter … … 40 41 one version of the functor value is used. 41 42 42 Hence, this moduleimplements a functor, typedlists, depending on a43 Hence, this library implements a functor, typedlists, depending on a 43 44 module parameter which must supply two exports, named equ? and type?. 44 45 The 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. 46 these two routines  defines immutable typed lists. 48 47 49 48 Note, that these typed lists don't pass the list? predicate. They … … 51 50 cases from the datatype module. 52 51 52 As an application of the typedlists functor, this library defines 53 another functor, typedsets, which depends on a second module, being 54 generated by the typedlists functor, which generates immutable typed sets, 55 being implemented as equivalence classes of immutable typed lists. 56 53 57 === typedlists 54 58 55 the functor's name, exporting typed lists and (typed) sets. 56 57 === Usage 59 the first functor's name, generating immutable typed lists when applied 60 to a module which provides the proper type? and equ? symbols. 61 62 === typedsets 63 64 the second functor's name, generating immutable typed sets when applied 65 to two modules, the first one providing the proper type? and equ? 66 symbols, the second being generated by invoking typedlists with the 67 first module. 68 69 === Usage of typedlists 58 70 59 71 <enscript highlight=scheme> … … 62 74 63 75 ;; define argument module 64 (module argname(type? equ?)76 (module vals (type? equ?) 65 77 (import scheme) 66 78 (define type? ...) … … 68 80 69 81 ;; apply functor 70 (module val name = (typedlists argname))82 (module vallists = (typedlists vals)) 71 83 72 84 ;; import the functor 73 (import val name)85 (import vallists) 74 86 75 87 ;; now use the generated routines … … 328 340 Then you can use bind and friends and freely mix typed lists with other 329 341 sequence types. 342 343 === Usage of typedsets 344 345 <enscript highlight=scheme> 346 (requirelibrary datatype) 347 (import typedlists typedsets datatype) 348 349 ;; define first argument module 350 (module vals (type? equ?) 351 (import scheme) 352 (define type? ...) 353 (define equ? ...)) 354 355 ;; apply typedlists functor to provide second argument module 356 (module vallists = (typedlists vals)) 357 358 ;; apply typedsets functor 359 (module valsets = (typedsets vals vallists)) 360 361 ;; import the modules 362 (import vallists valsets) 363 364 ;; now use the generated routines 365 </enscript> 330 366 331 367 === Generated functions of the set datatype … … 545 581 ;;; numbersets 546 582 ;;;  583 ;; generate module and import it 584 (module sets = (typedsets nums lists) 585 (import sets) 586 587 ;; apply it 547 588 (set= 548 589 (typedlist>set (typedlist 1 2 1 3 2 3)) … … 630 671 == Last update 631 672 632 Aug 18, 2014673 Aug 22, 2014 633 674 634 675 == Author … … 669 710 == Version History 670 711 712 ; 1.3 : functor split in two 671 713 ; 1.2 : listconssorted added 672 714 ; 1.1 : listbind corrected, listin? added
Note: See TracChangeset
for help on using the changeset viewer.