Changeset 21990 in project

12/14/10 19:58:42 (10 years ago)
Moritz Heidkamp

gazette #16: modules omelette (RAW)

1 edited


  • gazette/src/issues/

    r21946 r21990  
    8686== 4. Omelette Recipes
    88 This place has been intentionally left blank.
     88This week I'd like to give you a brief introduction to
     89[[|Chicken's module system]]. Modules
     90form a ''namespace'' or ''environment''. Basically you can think of
     91modules as containers for bindings. Also you have full control over
     92which bindings are exported from a module. This way you don't have to
     93worry about polluting the global namespace with auxiliary bindings. No
     94silly name prefixes anymore! An example illustrates this best:
     96<enscript language="scheme">
     97(module palindrome
     99  (palindromify palindrome?)
     101  (import chicken scheme srfi-13)
     103  (define (palindromify s)
     104    (string-append s (string-reverse s)))
     106  (define (palindrome? s)
     107    (let* ((len (string-length s))
     108           (half (quotient len 2)))
     109      (if (odd? len)
     110          (palindrome? (string-append (string-take s half)
     111                                      (string-drop s (+ half 1))))
     112          (string=? (string-reverse (string-take s half))
     113                    (string-drop s half)))))
     117As you can see, it's pretty straight forward. Unsurprisingly, the
     118{{module}} form introduces a module definition. The first symbol
     119{{palindrome}} is the module's name, followed by a list of symbols to
     120be exported from the module: {{(palindromify palindrome?)}}. These
     121symbols have to be bound somewhere inside the module. The rest of the
     122form is regular Scheme establishing these bindings. The first thing we
     123have to do is {{(import chicken scheme srfi-13)}}. Yes, this means a
     124module's environment is completely blank initially (except for the
     125{{import}} and {{use}} forms) thus we even have to import {{scheme}},
     126yet! This is an especially nice way to create an environment for a
     127DSL: No regular Scheme bindings are going to get in your way. In our
     128case we stick to Chicken flavored Scheme and some SRFI-13 goodness to
     129define a few palindrome procedures.
     131How to use this module now? Well, just import it like any other
     132module. Try sticking the above code into a {{palindrome.scm}} file,
     133{{load}} it in a {{csi}} REPL and then {{(import palindrome)}}:
     135 #;1> (load "palindrome.scm")
     136 ; loading palindrome.scm ...
     137 #;2> (import palindrome)
     138 #;3> (palindrome? "chicken")
     139 #f
     140 #;4> (palindromify "fo")     
     141 "foof"
     142 #;5> (palindrome? (palindromify "fo"))
     143 #t
     145See, no rocket science in that, though some basic linguistics. The
     146best thing is that the modules we imported into our {{palindrome}}
     147module are not visible in the environment our module is imported
     148into. So if our REPL environment hadn't {{srfi-13}} imported before
     149importing our module, it still won't have it afterwards. Nice!
    90151== 5. About the Chicken Gazette
Note: See TracChangeset for help on using the changeset viewer.