Programming - Representing Objects PDF

Title Programming - Representing Objects
Course Introduction to programming
Institution University of Greenwich
Pages 4
File Size 151.1 KB
File Type PDF
Total Downloads 13
Total Views 144

Summary

Download Programming - Representing Objects PDF


Description

Programming - Representing Objects Assignment (set! name expression) Changes the value bound to a name to the value of an expression. The name must already be defined and exist in the environment which set! is evaluated. The Substitution Model To evaluate a compound procedure, evaluate the body of the procedure with each formal parameter replaced by the value of the corresponding argument. There are problems with global variables to store state. There may be multiple instances of items and global variables decrease modularity. To work round this, you can encapsulate state within an object. An object must have internal state and be active. You tell the object what to do with the state rather than do it directly. In Scheme, objects are represented by procedures with local state variables. There are 4 programming paradigms:    

functional imperative logic object-orientated (sometimes called message-passing)

The Environment Model of Evaluation The substitute model doesn't work when assignment is involved. An environment is made of frames, where frames are tables of bindings (names and values) and a pointer to the enclosing environment. In a frame, a variable can be bound, unbound, the first binding or a "shadow" binding (overriding any earlier bindings). A procedure is the code and pointer to an environment. This is the environment in which the lambda expression defining the pointer was evaluated.

e.g., "blood-donor" is an object which has state (quantity of blood) and responds to "drain" requests. A constructor of blood donors takes a quantity of blood and returns a blood donor object. This object is itself a procedure which responds to requests (drain, etc) and then returns the procedure to do that event, which itself may take multiple arguments. An object needs to be defined with initial states, the object procedure and request handling procedures. The object constructor should return the object. Mutators Primitive Data Compound Data Creation 1, 'x, #f Constructors cons Selectors car, cdr Assignment set! Mutators set-car!, set-cdr! Pointers Box and Pointer Diagrams (cons x y) creates a new pair

The contents of the pair are pointers. A list terminator can be represented as follows:

e.g., '(1 2 3) is the short form of (cons 1 (cons 2 (cons 3 (nil))))

Equality =, two numerical values are the same eq?, two pointers are the same equal?, two structures are the same (here, it is the representation of the structures that are compared) Say we create 2 structures, (cons x 'a 'b) and (cons z1 x x)

We can now do (set-car! (cdr z1) 'boom) which changes 'a in the structure to 'boom. This now makes z1 (('boom 'b) 'boom 'b) and x is now ('boom 'b). However, if we (cons z2 (list 'a 'b) (list 'a 'b)), this makes z1 equal? to z2, but now (set-car! (cdr z2) 'boom) gives us (('a 'b) 'boom 'b) Association List An association list is a list of records, each of which consists of a key and a value. Each record is a pair formed by (cons key value). e.g., ((answer . 23) ('y . #t) ('z . "Hello")) associates answer with 23, y with #t and z with "Hello". The procedure (retrieve key associations) returns the first pair in associations which has key in its car or returns nil if there is no such record....


Similar Free PDFs