Com S 342 meeting -*- Outline -*- * Expressions in Scheme (and other languages) go over the syntax quickly, since covered in "designing a language" unit ** fundamental expressions use to give categories, and as an overview of Scheme ------------------------------------------ SYNTACTIC PARTS OF A PROGRAMMING LANGUAGE program definition statements expressions literals (#t, #\c, "a str") variable references (x, ls) procedure calls ( (f x), (car ls) ) ------------------------------------------ Note: every parenthesis is important; you can't leave any out, or put in any extra. Q: What's the translation of 5 + 6 + 7 into Scheme? Q: How would sqrt(cos(x + 5)) be translated into Scheme? Q: What's the syntax of a procedure call in Scheme? ::= ( {}*) Q: What's a rule for forming the Scheme from the algebraic notation? This is an example of regularity (as mentioned in the designing-a-language unit). ** definitions, programs, read-eval-print loop (quickly) *** definitions ------------------------------------------ DEFINITIONS Scheme syntax examples (define pi ; TYPE: number 3.14159) Terminology: - special form - keyword ------------------------------------------ ... (define greeting "Welcome!") (define is-pi? (lambda (n) (= n pi))) (deftype add1 (-> (number number) number)) (define add1 (lambda (n) (+ n 1))) Special forms - provide the "magic" - don't evaluate their all arguments in random order, as do procedures - similar to macros Q: What is like this in C++? Pascal? *** programs ------------------------------------------ PROGRAMS Scheme syntax: a series of definitions and expressions Example (define pi 3.14159) (define greeting "Welcome!") (define is-pi? (lambda (n) (= n pi))) (display greeting) (newline) (is-pi? pi) Semantics: Terminology - top level (pi vs. n) ------------------------------------------ ... execute from top (start of file) to end ------------------------------------------ READ-EVAL-PRINT LOOP What the interpreter does (exit) ---> read ----> ^ \ / v print eval ^-----/ ------------------------------------------ Go to the computer and show how to use Show what happens when do display ** if-expressions (1.1.3) ------------------------------------------ IF-EXPRESSIONS Transcript of Scheme examples > (if #t 3 4) > (define x 2) > (if (zero? x) (+ x 3) x) > (if (< x 0) (- x) x) ------------------------------------------ ... 3 ... 2 ... 2 Q: So what's the Scheme syntax? Q: Is the else-part required? Why would it be? Q: How does this differ from an if statement in C++ or Java? Q: What's this like in C/C++/Java? Anything like this in Visual Basic? (like e1 ? e2 : e3 in C/C++/Java)