Changeset 21992 in project

12/14/10 20:40:21 (10 years ago)
Moritz Heidkamp

gazette #16: add explicit internal binding to module example to better illustrate that it is not exported and differentiate modules from eggs as well as require-library, require-extension and use (thanks to Alan Post for the suggestions)

1 edited


  • gazette/src/issues/

    r21991 r21992  
    9999(module palindrome
    101   (palindromify palindrome?)
     101(palindromify palindrome?)
    103   (import chicken scheme srfi-13)
     103(import chicken scheme extras srfi-13)
    105   (define (palindromify s)
    106     (string-append s (string-reverse s)))
     105(define max-length 100)
     107(define (palindromify s)
     108  (string-append s (string-reverse s)))
    108   (define (palindrome? s)
    109     (let* ((len (string-length s))
    110            (half (quotient len 2)))
    111       (if (odd? len)
    112           (palindrome? (string-append (string-take s half)
    113                                       (string-drop s (+ half 1))))
    114           (string=? (string-reverse (string-take s half))
    115                     (string-drop s half)))))
     110(define (palindrome? s)
     111  (let* ((len (string-length s))
     112         (half (quotient len 2)))
     113    (cond ((> len max-length)
     114           (error 'palindrome?
     115                  (format "palindromes may not be longer than ~A characters" max-length)))
     116          ((odd? len)
     117           (palindrome? (string-append (string-take s half)
     118                                       (string-drop s (+ half 1)))))
     119          (else (string=? (string-reverse (string-take s half))
     120                          (string-drop s half))))))
    129134DSL: No regular Scheme bindings are going to get in your way. In our
    130135case we stick to Chicken flavored Scheme and some SRFI-13 goodness to
    131 define a few palindrome procedures.
     136define a few palindrome procedures. Note that we define a
     137{{max-length}} of 100 characters inside our module without exporting
     138it. This means users of our module will not be able to touch that
     139limit. Further note that code inside the {{module}} form is usually
     140not indented since usually there's only one module definition spanning
     141the whole file.
    133143How to use this module now? Well, just import it like any other
    144154 #;5> (palindrome? (palindromify "fo"))
    145155 #t
     156 #;6> max-length
     158 Error: unbound variable: max-length
    147161See, no rocket science in that, though some basic linguistics. The
    150164into. So if our REPL environment hadn't {{srfi-13}} imported before
    151165importing our module, it still won't have it afterwards. Nice!
     167One thing that might cause confusion is that Chicken eggs are not
     168equal to modules, although many eggs consist of just one module. The
     169difference is that eggs may contain any kind of Chicken Scheme code,
     170be it only one module, many modules, one or more programs or just some
     171raw definitions. It's all up to the egg author. Then you have
     172accompanying egg specific forms like {{require-library}} which loads a
     173module from an egg and {{use}}/{{require-extension}} which load and
     174then import egg modules. How these are used and how eggs are built is
     175beyond the scope of this recipe but you may want to check out
     176[[|the eggs tutorial on the wiki]].
    153178== 5. About the Chicken Gazette
Note: See TracChangeset for help on using the changeset viewer.