In The Schematics of Computation, Manis and Little discuss the concept of a module, as described by Niklaus Wirth. A module is "a collection of definitions ... that perform some set of services for the programmer. Modules keep some definitions private. Private definitions include procedures and other definitions that are not intended to be called by the outside world, but only by other procedures in the module. Other definitions are exported (made visible to the rest of the program) and may be used freely there. [p. 150]"
To implement this notion of a module in Scheme, Manis and Little define their own software package for use within Scheme.
(module directory
(export make-directory add-name look-up show)
(define make-directory
(lambda init-list
(if (null? init-list)
'(())
init-list)))
(define add-name
(lambda (dir . name-entry)
(if (null? dir)
(list name-entry)
(if (and (null? (cdr dir)) (eq? (car dir) nil))
(begin ;; if list is empty, just add name-entry at head
(set-car! dir name-entry)
;; return full list
dir)
(begin
;; first make a complete list with elements of dir,
;; then add new entry at the start
(set-cdr! dir (cons (car dir) (cdr dir)))
(set-car! dir name-entry)
;; return full list
dir)))))
(define look-up
(lambda (dir name)
(assoc name dir)))
(define show
(lambda (dir)
dir))
)
Initialization of a directory for the Math/Computer Science faculty uses
the statement
(define math-dir (make-directory
'("Arnold Adelberg" 4201)
'("Marc Chamberland" 4207)
'("Eugene Herman" 4202)
'("Charles Jepsen" 4203)
'("Charles Jones" 4204)
'("Emily Moore" on-leave)
'("Thomas Moore" on-leave)
'("Samuel Rebelsky" 4410)
'("John Stone" 3181)
'("Henry Walker" 4208)
'("Royce Wolf" 4209)))
(load "/home/walker/153/book-code/vslib/module.scm")
(show math-dir) (look-up math-dir "John Stone") (look-up math-dir "George Apostle") (add-name math-dir "Pamela Ferguson" 1234) (show math-dir)In each case, describe the results of the operation.
(math-dir 'show) (math-dir 'lookup "John Stone") (math-dir 'lookup "George Apostle") (math-dir 'add "Pam Ferguson" 1234) (math-dir 'show)while the corresponding operations using the text's modules have the form:
(show math-dir) (look-up math-dir "John Stone") (look-up math-dir "George Apostle") (add-name math-dir "Pamela Ferguson" 1234) (show math-dir)Discuss how this different format reflects differences in the approaches for implementation.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/153/lab-adt2.html