;; -------------------------------------------------------------------------- ;; | | ;; | FILE : reading-v2.rkt | ;; | AUTHOR : Eugene Wallingford | ;; | CREATION DATE : 2017/02/16 | ;; | | ;; | DESCRIPTION : The second version of the procedures defined in | ;; | Syntax procedures help us write code that hides | ;; | the implementation details of a data abstraction. | ;; | The code also says what it means. | ;; | | ;; | REQUIRES : No external definitions. Rackunit for tests. | ;; | | ;; -------------------------------------------------------------------------- #lang racket (require rackunit) ;; -------------------------------------------------------------------------- ;; the "after" picture: code for searching a binary search tree ADT, ;; using syntax procedures (define empty-tree? null?) (define node-value first) (define left-subtree second) (define right-subtree third) (define path (lambda (n bst) (cond ((empty-tree? bst) (error 'path "number not found!")) ((< n (node-value bst)) (cons 'left (path n (left-subtree bst)))) ((> n (node-value bst)) (cons 'right (path n (right-subtree bst)))) (else ;; we are sitting on it! '())))) (define example-tree '(14 (7 () (12 () ())) (26 (20 (17 () ()) ()) (31 () ())))) (check-equal? (path 17 example-tree) '(right left left)) ;; --------------------------------------------------------------------------