;; -------------------------------------------------------------------------- ;; | | ;; | FILE : reading-v1.rkt | ;; | AUTHOR : Eugene Wallingford | ;; | CREATION DATE : 2017/02/16 | ;; | | ;; | DESCRIPTION : The first version of the procedures defined in | ;; | the reading assignment at the end of Session 12. | ;; | It exposes the implementation details of a | ;; | data abstraction. | ;; | | ;; | REQUIRES : No external definitions. Rackunit for tests. | ;; | | ;; -------------------------------------------------------------------------- #lang racket (require rackunit) ;; --------------------------------------------------------------------------- ;; using syntax procedures for a point data type (define square (lambda (x) (expt x 2))) ; (define distance ; (lambda (x y) ; (sqrt (+ (square (- (car x) ; (car y))) ; (square (- (cdr x) ; (cdr y))))))) (define point->x car) (define point->y cdr) (define distance (lambda (point1 point2) (sqrt (+ (square (- (point->x point1) (point->x point2))) (square (- (point->y point1) (point->y point2))))))) ;; -------------------------------------------------------------------------- ;; the "before" picture: code for searching a binary search tree ADT (define path (lambda (n bst) (if (null? bst) (error 'path "number not found!") (if (< n (first bst)) (cons 'left (path n (second bst))) (if (> n (first bst)) (cons 'right (path n (third bst))) '()))))) (define example-tree '(14 (7 () (12 () ())) (26 (20 (17 () ()) ()) (31 () ())))) (check-equal? (path 17 example-tree) '(right left left)) ;; --------------------------------------------------------------------------