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) (cond [(empty? l1) l2] [(cons? l1) (make-cons (cons-first l1) (append (cons-rest l1) l2))])) (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) (cond [(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" (make-empty) (get-all-exits (make-hall (make-exit "moon") (make-dead-end) (make-exit "sun"))) "should be" (make-cons "moon" (make-cons "sun" (make-empty)))
Last update: Friday, February 6th, 2004mflatt@cs.utah.edu |