![]() |
![]() |
|
home—info—lectures—exams—hws—archive
Scheme 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 scheme:)
(/ 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 scheme 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 scheme:
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 scheme:
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—info—lectures—exams—hws—archive
| ©2009, Ian Barland, Radford University Last modified 2009.Sep.07 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |