![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
Review from yesterday:
classes of languages, one common representative, and its main features?
Some concepts to keep straight:
- value : a valid answer to a question. can be returned, passed ("a piece of information represented")
- expression : code that evaluates to a value (hence the word "evaluate")
Every value is trivially an expression (in most languages; but not e.g. SQL tables, grrr).
- variable : a name (identifier) which can be associated with a value.
A variable is a simple
- type : a set of values (er, a name for that set).
*Both* variables and values have associated types.
and while we're at it:
- statement : code that, when executed, changes the state (define a variable, ...)
This covers many constructs (e.g. Java class-definitions are statements; method-calls are expressions(*),
though not quite all (packages, ... ), but we'll focus on these.
(*) well, calling a void-method [with a semicolon] is a statement, and never an expression;
most method-calls are expressions or statements;
infix operators are only expressions and not statements.
Point out these parts in a sample Java program.
Btw:
----
String s;
s = "hello, everybody";
s.substring(1,3)
"hello".substring(1,3) // is this allowed?
// What about semicolons in the above?
Racket expressions for...:
sqrt(25) (sqrt 25)
2^30 (expt 2 30)
2+3 (+ 2 3)
2+3*4 (+ 2 (* 3 4))
2 + (3 * 4 + 5) * 6 (+ 2 (* (+ (* 3 4) 5) 6))
2 + 3 + 4 + 5 (+ 2 3 4 5)
2
Trade-offs, of prefix vs infix?
(You can construct these top-down, or bottom up;
when evaluating / figuring out what some epxression returns,
work bottom-up, a.k.a. from the innermost-parens outward)
Note that
sqrt : number -> number
expt : number, number -> number
+ : number, number -> number
sqrt(max(5,7)) + 2 (+ (sqrt (max 5 7)) 2)
Some string-handling functions:
string-append : string, string -> string
substring : string, number, number -> string
string=? : string, string -> boolean
A note on numbers:
(sqrt 2) -- inexact
Cf. (both Java and racket:)
(/ 2 300)
(* (/ 7 25) 25)
(sqrt -4)
Inexacts, in Java:
*must* be 32-bit arithmetic; if you do 'better' you're not Java.
(if your 2billion + 2billion = 4billion in Java, it's not Java.)
Portability considered more important than incrementally increasing accuracy.
================== for next time:
Review: uses prefix notation, exact rationals [compare racket vs Java vs ...]
Let's write some functions:
I. define
(define a 2)
(define b 8)
(define c 42)
; quadratic expression
II. define a function
(define (root1 a b c)
...)
III. practice: string functions
blend : string, string -> string
IIIa: expected results
IIIb: write
IIIc: expected results w/ 'string=?'; expected results w/ check-expect
==== lect02a: ====
IV: practice: blend-middle; function-call-function
V: practice: area-of-disk; area-of-ring
Use check-within
a(r) = pi*r^2 area-of-disk
area-of-ring(r2,r1)
VIa: types in racket:
number (integer, rational, inexact;
functions +, /; number?, integer?, exact?, rational?; zero? positive?);
string (string-append, string=?, string?)
boolean (boolean=?, not, and, or)
symbols; only functions are symbol? and symbol=?; symbol->string
VIb: images, image-primitives
circle, rectangle
overlay/xy, image-width, image-height
VII: write image-functions
(define (align-h img1 img2)
(overlay/xy img1 (image-width img1) 0 img2))
Write align-v
board2x2
board4x4
board8x8
Note conversion functions have suggestive names:
number->string, string->number
btw, compare these function calls to the sh:
find . -name \*.ps -print
find . -name \*.ps -print | grep -i wimpout
Note that args can be passed by position (first arg to 'find'),
or by name (the '-name' flag, in this case)
What about '-i wimpout'? What drawbacks to differenting a real filename
from flags starting with '-'?
Try:
cat > -i
hello
^D
grep hel -i
grep -- hel -i
What about a program, where the filname is in a variable?
Back to racket:
let x = 3.
Next time:
strings, and calling string functions (substring; string-append);
images, and calling image functions
(circle; rectangle; overlay/xy; empty-scene; place-image)
functions: f(x) = x+3
(define (align-h img1 img2)
(overlay/xy img1 (image-width img1) 0 img2))
home—lectures—exams—hws—breeze (snow day)
| ©2011, Ian Barland, Radford University Last modified 2011.Sep.02 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |