210 likes | 244 Views
Data Abstraction. CMSC 1150 Introduction to Computer Programming October 2, 2002. Roadmap. Recap: Compound expressions & Procedural Abstraction Data Abstraction Motivation: Compound Data Abstract Interface: Constructors and Selectors Concrete Implementation with define-struct
E N D
Data Abstraction CMSC 1150 Introduction to Computer Programming October 2, 2002
Roadmap • Recap: • Compound expressions & Procedural Abstraction • Data Abstraction • Motivation: Compound Data • Abstract Interface: Constructors and Selectors • Concrete Implementation with define-struct • Abstraction Barriers • Good Design: Data definitions and contracts • Summary
Administrivia • Add yourself to the course mailing list: • http://mailman.cs.uchicago.edu 11500-1 • TA office hours: • Leandro Cortes: Thurs 2-4, Ry178 • Yu “Tony” Hu: Fri 2-4, Ry 178 • Dinoj Surendran: Wed 3-4, Fri 10-11am, Eckhart 006 • First homework: Due Monday • Dr Scheme note: “Language” set to R5RS
Recap: Expressions and Procedures • Primitives: E.g. integers • Primitive operators: +,-,*,.. • Primitive expressions: (+ 1 2), (* 3 4) • Compound expressions: • (* (+ 1 2) (+5 6)) • Procedural abstraction: (define (square x) (* x x)) • Conditionals: if, cond • Issues: Substitution model, Evaluation order
Compounding & Abstraction • Procedures and Expressions • Combine primitives • Create more complex, powerful expressions • Abstractions: • Procedures as black boxes, apply to different input • E.g. square procedure: • (define (area r) (* pi (square r))) • Doesn’t matter how implement square as long as it returns the square of the input.
Compound Data & Abstraction • Similar issues arise for data • Represent things more complex than integers • Similar to building more complex functions • Objects have many facets • Abstract away from implementation • Any structure that behaves correctly
Compound Data: Motivation • Real world objects often have many facets • Example: Specifying a point in a plane • (x,y) coordinate pair • Create one data structure that holds BOTH • In use, often want x- or y- coordinate • E.g. to compute distance from origin, slope of line defined by two points, etc • Need accessors for each facet
Compound Data: Structures • Example: (x,y) coordinates • (make-posn x y) • Takes two numbers • Combines into a posn structure • posn can be input or output of procedure • E.g. (distance-to-0 (make-posn 5 0)) => 5 • (distance-to-0 (make-posn 3 4)) => 5
Accessing Components • To compute distance-to-0 • Need x and y coordinate values • Two procedures: posn-x, posn-y • (posn-x (make-posn 1 3)) => 1 • (posn-y (make-posn 1 3)) => 3 • (define (distance-to-0 a-posn) • (sqrt (+ (square (posn-x a-posn)) • (+ (square (posn-y a-posn))))
Abstraction • How do make-posn, posn-x, and posn-y work? • As users, we don’t need to know • Key: make-posn combines x and y • posn-x, posn-y return coordinates • Any implementation with this behavior is fine
Interface • Define an interface between abstract use and concrete implementation of structure • Two types of procedures: • Constructors: e.g. make-posn • Selectors: e.g. posn-x, posn-y • Implement abstract data via concrete representation
A Concrete Implementation • (define-struct posn (x y)) • Structure definition creates 3 procedures: • Constructor: make-posn • Selector: posn-x • Selector: posn-y • Discussion: Other structures
Example: Operations on Points • (distance-to-0 a-posn) • (line-length a-posn1 a-posn2) • (line-slope a-posn1 a-posn2) • etc….
Abstraction Barriers • Separate programs that use abstraction from those that implement abstraction Points in problem domain Distance-to-0 line-length line-slope Points as x,y coordinates make-posn posn-x posn-y Points as structures define-struct However structures are implemented
Why Abstraction? • Can make programs much easier to maintain and modify • Consider alternate implementations • Limit changes to small # of interface procedures • But only successful if maintain abstraction
Good Design: Data Definition • Problem: (make-posn ‘dog ‘cat) • Clearly wrong • Would blow up in distance-to-0, line-length, etc • Issue: Guarantee that given correct types of input to constructor, processing output of selectors should yield intended result • Solution: Data Definition • Establish agreement between programmers and users
Data Definition • What is it? • Combination of Scheme and English specifying • How to use a class of structures • How to construct elements of this class • Example: posn • A posn is a structure (make-posn x y) • where x and y are numbers • Discussion: Other definitions
Summary • Compound data & abstraction • Parallel to use of compound expresssions • Capture multiple facets of objects • Interface: Constructors and Selectors • Abstraction: Allow users of data to ignore implementation • Abstraction barriers • Concrete Implementation with define-struct • Data definitions: Establish covenant between users and programmers of data for behavior and input
Next Time • Today: Finite compound data • Friday: Infinite compound data • Lists: constructors and selectors • Recursion