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!)

120 views2 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers

Related Documents

Related Questions