lect05 (aka week02b) -- Sep.04 (Wed) ==== compile vs interpret Finish last slide from lect03-slides.rkt: compile vs interpret. discuss: jvm; reverse-engineering byte-code / machine code; ... . ==== Racket === Types: number (integer, rational, real, complex; exact/inexact), string, char, #\a (always feels awkward to me) boolean, symbol 'yellow [yep, only one quote-char, not two] Symbols are like strings, but you can't do string-like things on it, like `substring` or `upcase`. Use them (say) for keeping track of Can serve as enumerated types, e.g. 'red 'green 'yellow (for a stoplight controller) Also, just as to represent something else (like, you know,... a symbol.) 'saab 'vw 'porsche 'volt ...[not necessarily pre-ordained or even finite] later, more about: structs functions (!) === calling functions (+ 2 3) (substring "hello" 2) General syntax(*): ( [func-id] [expr] [expr] ... ) A note on prefix/uniformity: it's the only one way to call a function in racket; Cf. Java using both infix *and* method-call. When calling functions, think of lifting the paren around to just-after the function. The lack-of-commas is also driven by lisp/scheme/racket's same minimalist attitude. (*) Once we get to intermediate-student, we'll revise this syntax: the `func-id` can actually be any expr, so long as it evals to a function. === aside Why do we use beginning-student? Not just to be disparaging. Rather: consider Java's `if (keepGoing = false) { ... }`. This *can't* be a compiler error, even though it is almost certainly not what the programmer intended. The *reason* is that java's assignment operator returns a value: int a = b = c = 17; int a = (b = (c = 17)); It's a cute idea, but I never really use it in practice. But I *have* seen beginners pounding their head against the wall about `if (... = false) ...`. Beginning-racket can tag `((` as an immediate error, even though it can be legal in advanced. Also, the error message will make more sense. === nesting expressions (+ 2 3 (* 4 5) 6) Q: can't function-nesting get really hairy? Yes, but also in java: ("hello".concat("aloha")).substring( Math.sqrt( Math.pow(2,3) + 1) ) vs racket (substring (string-append "hello" "aloha") (sqrt (+ (expt 2 3) 1))) Be sure to indent clearly! In *either* langauge, either have *all* args on single line, or one *per* line. In either language, you can use vars to name a sub-part of the overall computation. We'll see more on that later. === `define` Defining a constant: (define a 23) (define WIDTH 200) (define SCHOOL-NAME "Radford") === signatures: The "signature" of a function is just its type -- its domain and codomain. (As mentioned yesterday, in calc classes they use the same notation, "f : R -> R" ) Give signatures for: * expt : number, number -> number * = : number, number -> boolean * substring : string, int -> string (or better than int: `natnum`, since it can't be negative.) What are signatures for the following functions? * string=? * number->string * not * if (?!?!) ==== Teaser: Defining our own function: (define (triplify x) (* 3 x)) ---- for next time -- sketch ---- define a function: *monogram: "Ian" "Barland" -> "i.b." initials: / acronymize "Self-contained underwater breathing apparatus", "radio detection and ranging", "light-amplified stimulated emission of radiation" write 'string->initial'; then hint at writing 'acroynimize': (apply string-append (map initialize (regexp-split ... #rx"\s-"))) has-suffix? (towards pluralize) ------- per data type: ;1. data def'n ;2. examples of the data ------- per function: 3. tests 4. : signature, head, description ;5. template (for body) template-with-examples / inventory 6. complete the body-expression Refactor to make-initial? [should I include '.' or not? For refactoring, yes. For acronym, no.] these steps are "design recipe" cond: silly example: rock/paper/scissors (`rps