previous up next     contents index
Next: Creating Compound Units Up: Creating Units Previous: Creating Units

Examples

This unit defined below imports and exports no variables. Each time it is invoked, it prints and returns the current time in seconds:gif

  (define f1@
     (unit
       (import) (export)
       (define x (current-seconds))
       (display x) (newline) x))

The expression below is syntactically invalid because cuurent-secoods is not a built-in procedure:

  (define f2@
     (unit
       (import) (export)
       (define x (cuurent-secoods))
       (display x) (newline) x))
but the next expression is fine because the unit expression is in the scope of the let-bound variable:
  (define f2@
     (let ([cuurent-secoods current-seconds])
       (unit
         (import) (export)
         (define x (cuurent-secoods))
         (display x) (newline) x)))

The unit defined below implements a background appointment-checking engine. It imports a check-datebook procedure, an appointment-description selector procedure, and a notify-user procedure. These imported procedures are exported by two other units -- an appointment database unit and a user interface unit - but this information is not part of the appointment-checker@ unit specification:

  (define appointment-checker@
    (unit
       (import check-datebook appointment-description notify-user)
       (export last-check-date)
       (define last-check-date #f)
       (thread
         (lambda ()
           (let loop ()
             (sleep 100)
             (set! last-check-date (current-seconds))
             (let ([v (check-datebook last-check-date)])
               (when v
                 (notify-user (appointment-description v))))
             (loop))))))

This simple unit can be used for the appointment checker's user interface:

  (define user-interface@
    (unit
      (import)
      (export notify-user-by-simple-display)
      (define notify-user-by-simple-display
        (lambda (s)
          (display s)
          (newline)))))



PLT