lec19 - ; CSE 341 Spring 2008, Lecture 19 (abstraction with...

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

View Full Document Right Arrow Icon
;; CSE 341 Spring 2008, Lecture 19 (abstraction with dynamic types) ; ;; recall from lecture 12 we want a library that ;; manipulates only positive rational numbers ;; and shows them only in reduced form. ;; we use hidden functions gcd and reduce to do this ; ;signature RATIONAL_B = ;sig ; type rational (* type now abstract *) ; exception BadFrac ; val make_frac : int * int -> rational ; val add : rational * rational -> rational ; val print_rat : rational -> unit ;end ; ;; non-abstract version: hides gcd/reduce ;; but does not hide representation of rationals ;; so may need to recheck (or just re-establish invariants) at each entry point ;; and we still don't know if clients are actually assuming the implementation! ;; (and mutation is still a problem, particular if there is concurrency) (define-struct rat1 (num den)) (define pos-rat-funs1 (letrec ([gcd (lambda (x y) (cond [(= x y) x] [(< x y) (gcd x (- y x))] [#t (gcd y x)]))] [reduce (lambda (r) (let ([d (gcd (rat1-num r) (rat1-den r))]) (make-rat1 (quotient (rat1-num r) d) (quotient (rat1-den r) d))))] [make-frac (lambda (x y)
Background image of page 1

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

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

This note was uploaded on 10/12/2009 for the course CSE 341 taught by Professor Staff during the Spring '08 term at University of Washington.

Page1 / 3

lec19 - ; CSE 341 Spring 2008, Lecture 19 (abstraction with...

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