CS 6520 HW 3

A maze has named goals, dead-ends, and hallways that lead three directions. Here's a pseudo-Scheme data definition for mazes and for lists of strings, with associated functions. Questions follow.

; A list-of-string is either
;  - empty-los
;  - cons-los

; An empty-los is
;  (make-empty)
(define-struct empty ())

; A cons-los is
;  (make-cons string list-of-string)
(define-struct cons (first rest))

;; append : list-of-string list-of-string -> list-of-string
;;  Appends the first and second lists to produce a single
;;  list
(define (append l1 l2)
   [(empty? l1) l2]
   [(cons? l1) (make-cons
		(cons-first l1)
		(append (cons-rest l1)
(append (make-empty) (make-empty)) "should be" (make-empty)

(append (make-cons "a" (make-cons "b" (make-empty)))
	(make-cons "x" (make-cons "y" (make-empty))))
"should be" 
(make-cons "a"
	   (make-cons "b"
		      (make-cons "x" 
				 (make-cons "y" (make-empty)))))

; A maze is either
;  - exit
;  - dead-end
;  - hall

; An exit is
;  (make-exit string)
(define-struct exit (name))

; A dead-end is
;  (make-dead-end)
(define-struct dead-end ())

; A hall is
;  (make-hall maze maze maze)
(define-struct hall (a b c))

; get-all-exits : maze -> list-of-string
;  Returns a list of strings for the names of
;  all exits in the maze
(define (get-all-exits m)
   [(exit? m) (make-cons (exit-name m) (make-empty))]
   [(dead-end? m) (make-empty)]
   [(hall? m) (append (get-all-exits (hall-a m))
		      (append (get-all-exits (hall-b m))
			      (get-all-exits (hall-c m))))]))

(get-all-exits (make-exit "moon"))
"should be"
(make-cons "moon" (make-empty))

(get-all-exits (make-dead-end))
"should be"

(get-all-exits (make-hall (make-exit "moon")
			  (make-exit "sun")))
"should be"
(make-cons "moon" 
	   (make-cons "sun" (make-empty)))
  1. The function implementations don't match the data definitions. Breifly explain two different ways to fix the mismatch.
  2. Shouldn't get-all-exits simply use make-cons where it uses append?
  3. Why does append have only two tests, while get-all-exits has three?
  4. Is it obvious that the append function should use the list-of-string template? Why is the template applied to only l1?
  5. Translate the code to pseudo-Java with good object-oriented style.

Last update: Friday, February 6th, 2004