CS135 Midterm: Structures Compound data overview, Posn structures and defining your own structures. Includes data definition instructions and Template instructions (BOTH IMPORTANT ON BOTH MIDTERMS AND FINAL!)
Document Summary
The teaching languages provide a general mechanism called structures. In many situations, data is naturally grouped, and most programming languages provide some mechanism to do this. There is also one predefined structure, posn, to provide an example. Posn structures: constructor function make-posn, with contract, ;; make-posn: number number posn, always: make- name of structure . Selector functions posn-x and posn-y, with contracts: ;; posn-x: posn number, ;; posn-y: posn number, always: name of structure - field/data name . Constructor makes structure, selector extracts data from it. Ex) (define mypoint (make-posn 8 1)) constructor makes structure at (8,1) (posn-x mypoint)) selector extracts data, in this case 8 (posn-y mypoint)) selector extracts data, in this case 1. If posn wasn"t built in, we would define it like this: (define-struct posn (x y)) The arguments to the define-struct special form are: a structure name (e. g. posn), and a list of field names in parentheses.