animal - (display "I give up, what is it?...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
(define (animal node) (define (type node) (car node)) (define (question node) (cadr node)) (define (yespart node) (caddr node)) (define (nopart node) (cadddr node)) (define (answer node) (cadr node)) (define (leaf? node) (eq? (type node) 'leaf)) (define (branch? node) (eq? (type node) 'branch)) (define (set-yes! node x) (set-car! (cddr node) x)) (define (set-no! node x) (set-car! (cdddr node) x)) (define (yorn) (let ((yn (read))) (cond ((eq? yn 'yes) #t) ((eq? yn 'no) #f) (else (display "Please type YES or NO") (yorn))))) (display (question node)) (display " ") (let ((yn (yorn))) (let ((next (if yn (yespart node) (nopart node)))) (cond ((branch? next) (animal next)) (else (display "Is it a ") (display (answer next)) (display "? ") (let ((correctflag (yorn))) (cond (correctflag "I win!") (else (newline)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (display "I give up, what is it? ") (let ((correct (read))) (newline) (display "Please tell me a question whose ") (display "answer is YES for a ") (display correct) (newline) (display "and NO for a ") (display (answer next)) (display ".") (newline) (display "Enclose the question in ") (display "quotation marks.") (newline) (let ((newquest (read))) (if yn (set-yes! node (make-branch newquest (make-leaf correct) next)) (set-no! node (make-branch newquest (make-leaf correct) next))) "Thanks. Now I know better.")))))))))) (define (make-branch q y n) (list 'branch q y n)) (define (make-leaf a) (list 'leaf a)) (define animal-list (make-branch "Does it have wings?" (make-leaf 'parrot) (make-leaf 'rabbit))) (define (animal-game) (animal animal-list))...
View Full Document

This note was uploaded on 11/30/2010 for the course EECS 21281 taught by Professor Harvey during the Spring '10 term at University of California, Berkeley.

Page1 / 2

animal - (display "I give up, what is it?...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online