Textbook Notes (290,000)
CA (170,000)
UBC (3,000)
CPSC (20)
Chapter 2

CPSC 110 Chapter Notes - Chapter 2: Webex, Data Definition Language


Department
Computer Science
Course Code
CPSC 110
Professor
Gregor Kiczales
Chapter
2

This preview shows pages 1-3. to view the full 16 pages of the document.
webEx Notes - Module 2 - How to Design Data
Cond Expressions
- Multi-armed conditional
- Allows us to program conditional statements with more than 2 cases that are on the
same level
This is used instead of a nested if, when statements which are ‘on the same level’ (meaning that
cases are not a sub case of another but have the same weighting... ) using a nested if statment
would not be our best option. Cond expressions were designed for such cases.
For example:
When evaluating the weight of a person to be either underweight overweight or average, using a
cond statement would be deemed more useful instead of the nested if statements as they are all
on the same level (in terms of evaluation).
(define (function-name variable)
(cond [ Question Answer]
[ Question Answer]
[ Question Answer] ))
NB: ( ) could be used instead of [ ] … Racket treats them the same.
Example :
//Taken from the video
(define (aspect-ratio img)
(cond [(> (image-height img) (image-width img) ) “tall” ]
[(= (image-height img) (image-width img) ) “square” ]
[ else “wide” ] ))
NB: the else is used as the last option which reads if condition 1 or condition 2 is not met then
do this. In this case, if it is not tall, or square then it is wide.
Evaluating a cond expression:
(cond [ (> 1 2) “Bigger” ]
[ (= 1 2) “Equal” ]
[ (< 1 2) “Smaller”] )
Evaluate the first conditional statement
(cond [ false “Bigger” ]
[ (= 1 2) “Equal” ]
[ (< 1 2) “Smaller”] )
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Since the first conditional statement is false, it is deleted and we move to the next conditional
statement
(cond [ (= 1 2) “Equal” ]
[ (< 1 2) “Smaller”] )
Evaluate the 2nd conditional statement
(cond [ false “Equal” ]
[ (< 1 2) “Smaller”] )
Since the second conditional statement is false, it is deleted and we move to the next
conditional statement
(cond [ (< 1 2) “Smaller”] )
Evaluate the third conditional statement
( cond [true “Smaller”] )
The final output is
“Smaller”
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Data Definition
- Gives us everything we need to know about how to represent information as data
Layout
1. Possible structure definition //with compound data
2. ;; Data definition is..
3. ;; how to interpret the data
4. examples
5. ;; Template
6. ;; Template rules used
For example:
;; Time is Natural
;; interp. number of clock ticks since start of game
(define START-TIME 0)
(define OLD-TIME 1000)
#;
(define (fn-for-time t)
(... t))
;; Template rules used:
;; - atomic non-distinct: Natural
Types of data definition
simple atomic (distinct and non-distinct)
interval
enumeration
itemization
compound
self-reference
reference (to other data-type)
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version