Laboratory Exercises For Computer Science 153

An Alternative Implementation of ADTs

An Alternative Implementation of ADTs

Goals: This laboratory exercise introduces a different approach to the implementation of abstract data types, as defined in the previous.

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.

  1. Read Sections 3.4.2-3.4.3 and 4.1 in the textbook to expand upon the concepts of modules and abstract data types.
Within this concept of a module, we might define the department directory application of the previous lab as follows:
(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)))
  1. In order to use the book's module definitions, one must load some special definitions into the Scheme environment. This can be done within Scheme by typing
    (load "/home/walker/153/book-code/vslib/module.scm")
    
  2. After loading this macro package, copy the above defintions into Scheme, and check how this ADT can be used through the following commands:
    (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.

  3. Compare this implementation of a module with the implementation of an abstract data type in the previous lab.

    1. In each case, describe where data are stored.

    2. Describe how operations on the data are defined in each implementation.

    3. The syntax for applying operations differs in the two implementations. The ADT structure in the previous lab leads to statements:
      (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.

    4. Discuss to what extent data and operations may be public or private in each implementation.

This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/lab-adt2.html

created March 26, 1998
last revised March 27, 1998