Lecture 2: "Arithmetic"
Agenda
- arithmetic
- numerical data
- "operators" & functions
- variables
- prefix notation (vs. infix vs. postfix)
- arithmetic of Strings & Images
Notes
Arithmetic
- traditional def: numbers and the operations (+,-,/,*) defined over them
- our (broader) def: a collection of data and the operations (aka "functions") defined over them
- e.g., Boolean arithmetic { true, false } → and/or/not → { true, false }
Numerical data
- input/output (typically) for numerical functions
- Two classes of numerical data
- Exact (e.g., integral, fractional)
- Can choose between fractional/decimal representation in DrRacket (right click)
- Inexact (e.g., irrational numbers -- "pi", "√2")
- Indicated in DrRacket with
#i
prefix - Cannot be precisely represented on a computer ... insufficient memory!
- Indicated in DrRacket with
- Exact (e.g., integral, fractional)
Variables
- Racket has some built-in definitions for us; e.g.,
pi
⇒ 3.1415...,e
⇒ 2.1828... - can create our own "names" for constant values using
define
- e.g.,
(define mypi 3.14)
- we say
define
binds a variable to a value - convenient: saves typing, allows reuse, helps reduce errors
- e.g.,
"Operators" & Functions
operators vs. functions (e.g., "+" vs. "sin") -- no real difference; they are all just "functions" in Racket
functions are applied to input values, aka arguments, to produce an output value
- a "unary" function takes just one input
- a "binary" function takes two inputs
- the "arity" of a function is the number of inputs it takes
- e.g., a function with an "arity of 4" takes 4 inputs
functions, when applied to required inputs, create expressions which in turn evaluate to values
- e.g., (+ 3 4) ⇒ 7; (+ 3 (* 4 2)) ⇒ 11
- e.g., (sin pi) ⇒ 0; (sqrt (+ (sqr 4) (sqr 3))) ⇒ 5
- i.e., all expressions that involve function applications produce values!
- but, not all parenthesized expressions produce values --- e.g.,
define
is a "special form" which creates a variable binding ... it doesn't evaluate to anything
note: standalone values, aka "atomic" values, aka "atoms" (e.g., numbers) are also expressions (since they trivially evaluate to values, too)
Prefix notation (vs. infix vs. postfix)
usually write mathematical expressions like this: 3 + 4; or 3 + 4 * 2
- "infix" notation
- requires knowledge of operator precedence to evaluate correctly
can instead write operands first, followed by operation
- e.g., 3 + 4 ⇒ 3 4 +
- e.g., 3 + 4 * 2 ⇒ 4 2 * 3 +
- e.g., 4 * 6 + 2 * 3 ⇒ 4 6 * 2 3 * +
- known as "postfix" or "reverse polish" notation
- uses a "stack" to store operands that haven't yet been consumed by operations
- removes ambiguity of result (no longer need to know about operation precedence)
Racket uses "prefix" notation; operator comes first, followed by operands
- easy to convert from postfix to prefix: just write backwards!
- postfix 4 6 * 2 3 * + ⇒ prefix + * 3 2 * 6 4
- Racket requires full parenthesization, since it removes ambiguity when we don't know how many operands there are for a given function
- lets use take operators like
+
, which is typically a binary operator, and use it as a function with an arbitrary arity ≥ 2- e.g. (+ 1 2 3 4) ⇒ 10
- lets use take operators like
- easy to convert from postfix to prefix: just write backwards!
Arithmetic of Strings & Images
strings are sequences of characters
- e.g.,
"hello"
- we can "add" two strings together: string "concatenation"
(string-append "hello" "world")
⇒"helloworld"
- we can also determine the length of a string, get a substring, and convert between numbers and strings (see HtDP2e, 2.1.3)
- e.g.,
images are also a form of data
- can create them using functions such as
circle
,rectangle
andtriangle
- can paste them into our programs after copying them from our web browsers
- can bind variables to them using
define
- can create new images from existing ones using functions like
rotate
,scale
andflip-vertical
- can also combine multiple images to create new images using functions like
overlay
,beside
andoverlay/offset
- can create them using functions such as
but how do these functions work?!
- DON'T PANIC. Check out the Racket documentation --- search by function names, and make sure to read descriptions & examples for functions "provided from 2htdp/image"
- list of all image functions at http://docs.racket-lang.org/teachpack/2htdpimage.html
- note: need to first add "image.ss" teachpack from list of "HtDP/2e Teachpacks" and run