Com S 342 meeting -*- Outline -*- * Expressions in Scheme (and other languages) (1.1) Note: This studies Scheme from the perspective of languages in general, not just to program in Scheme. Students: note both the - terminology & perspectives - facts Keep drawing analogies to other languages (me too!) will help think about languages in general I don't think it's helpful to give all the definitions, which are in the book. Maybe give the students a sheet to fill them in, if think more important, but I think it's okay to just mention most. ** fundamental expressions (1.1.1) use to give categories, and as an overview of Scheme ------------------------------------------ SYNTACTIC PARTS OF A PROGRAMMING LANGUAGE ------------------------------------------ Q: What are the basic kinds of syntax in a programming language? / \ \ / \ \ | | / / \ \ | | | | | | 3.4 7 #t #f #\a "a string" *** terms relating to variables ------------------------------------------ TERMINOLOGY RELATING TO VARIABLES - x denotes 3 - x is bound to 3 - 3 is the binding of x - expressed values - denoted values - keywords vs. identifiers vs. reserved words ------------------------------------------ reserved words always are special in non-quoted contexts, keywords only special in some contexts (e.g., in Scheme and PL/I) *** procedure calls ------------------------------------------ PROCEDURE CALLS Scheme syntax examples Scheme: every parenthesis is significant no extra, no omitted Terminology: procedure (Scheme) function (math) application, combination operator, operand subexpressions arguments, actual parameters, actuals, parameters ------------------------------------------ ... (+ 3 4) (* 5 (+ 3 7)) (min x (add1 y)) compare to other languages: FORTRAN, ML, Smalltalk 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 def: a language feature is *regular* if examples: Scheme procedure call syntax FORTRAN numeric syntax counter-examples: a C function can't be declared to return an array type English verb past tenses ------------------------------------------ ... it is governed by rules that have no execptions. Q: What's an irregularity in English? change -> changed chomp -> chomped write -> wrote sing -> sung Q: Are programming languages more regular than natural languages? Q: Does that help in learning programming languages? ** definitions, programs, read-eval-print loop (1.1.2) *** definitions ------------------------------------------ DEFINITIONS Scheme syntax examples (define pi ; TYPE: number 3.14159) Terminology: - special form - keyword ------------------------------------------ ... (define greeting ; TYPE: string "Welcome!") (define is-pi? ; TYPE: (-> (number) boolean) (lambda (n) (= n pi))) (define add1 ; TYPE: (-> (number number) number) (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 ; TYPE: number 3.14159) (define greeting ; TYPE: string "Welcome!") (define is-pi? ; TYPE: (-> (number) boolean) (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 Pascal? Q: What's this like in C? Anything like this in Pascal? (like e1 ? e2 : e3 in C) nothing in Pascal