Some scheme Greg Sullivan 16410/16413 September 8, 2004 Outline Scheme syntax · Procedures A mix of scheme- recursion specific details, and general programming tutorial lists struct sept.8,2003 16.41016.413 Intro. to scheme 2
Some Scheme Greg Sullivan 16.410/16.413 September 8, 2004 / 2 Outline • • A mix of Schemegeneral programming tutorial Procedures – recursion – lists – structures specific details, and Sept. 8, 2003 16.410 16.413 Intro. to Scheme Scheme syntax • Data 1
Programming Languages · Control Conditionals Functions function calls Threads Data Scalars: booleans, numbers, strings Records/ structures Classes, types sept.8.2003 16.41016.413 Intro. to scheme Scheme A language for describing computational processes Precise sequence of steps used to infer new information from a set of data Governed by rules for: 1. Syntax: Legal expressions have rules by which they are constructed from simpler pieces 2. Semantics:(Almost)every expression has a value which is "returned"when an expression is evaluated 3. Every value has a type. sept.8,2003 16.41016.413 Intro. to scheme
Programming Languages • Control – Conditionals – Loops – Functions, function calls – Exceptions – Threads • Data – Scalars: booleans, numbers, strings – Records / Structures – Classes, types Sept. 8, 2003 16.410/16.413 Intro. to Scheme 3 / 4 Scheme A language for describing computational processes: – Governed by rules for: expression has a value, 1. Syntax: Legal expressions have rules by which they are constructed from simpler pieces. 2. Semantics “evaluated”. 3. Every value has a Sept. 8, 2003 16.410 16.413 Intro. to Scheme Precise sequence of steps used to infer new information from a set of data. : (Almost) every which is “returned” when an expression is type. 2
Syntax constants scalars abstractions(functions) " self-evaluating”) ambda(x)(什x1) 42. hello, world function calls 3.1415,#,# (+(succ x)4) (define X 32) “ special forms”( keyword…) variable references (if ( x max)x max),(set! X 44) t X sept.8.2003 16.41016.413 Intro. to scheme Read-Eval-Print Visible world (+3(*45) READ repres EVAL Applies rules Value of ey Execution world PRINT sept.8,2003 16.41016.413 Intro. to scheme 3
Syntax constants / scalars abstractions (functions) (“self-evaluating”) (lambda (x) (+ x 1) 42, “hello, world”, function calls 3.1415, #t, #f (+ x 4) definitions (define succ (lambda (x) (+ x )) (+ (succ x) 4) (define x 32) “special forms” (keyword …) variable references (if (> x max) x max), (set! x 44) x (let ((x 5)) (+ x 3)), etc. Sept. 8, 2003 16.410/16.413 Intro. to Scheme 5 / 6 Read-Eval-Print Execution world (+ 3 (* 4 5)) PRINT 23 EVAL Applies rules READ Sept. 8, 2003 16.410 16.413 Intro. to Scheme Visible world Internal representation for expression Visible world Value of expression 3
Language elements combinations / applications function calls A combination applies a procedure to a set of arguments +23) Close paren Other expressions Open paren Expression whose value Evaluate by getting values of sub expressions, then applying operator to values of arguments sept.8.2003 16.41016.413 Intro. to scheme Mathematical operators are just names (+35) 今8 (define fred + undef (fred 4 6) 10 .+ Is Just a name is bound to a value which is a procedure line 2 binds the name fred to that same value · Even more surprising (define+*) (+34) sept.8,2003 16.41016.413 Intro. to scheme
/ 7 Language elements – combinations / applications / function calls • applies a procedure to a set of arguments: (+ 2 3) • getting values of subexpressions, then applying operator to values of arguments is a procedure Close paren A combination Evaluate by Sept. 8, 2003 16.410 16.413 Intro. to Scheme Open paren Expression whose value Other expressions / 8 Mathematical operators are just names (+ 3 5) Î 8 Î undef Î 10 fred (define + *) (+ 3 4) (define fred +) (fred 4 6) • How do we explain this? • + is bound to a value which is a procedure Sept. 8, 2003 16.410 16.413 Intro. to Scheme •+ is just a name • line 2 binds the name to that same value • Even more surprising: 4
Language elements Procedural abstraction Need to capture ways of doing things use procedures parameters (lambda(x)(xx)k body To process something multiply it by itself Special form -creates a procedure and returns it as a value sept.8.2003 16.41016.413 Intro. to scheme Language elements Procedural abstraction Use a lambda expression anywhere you would use a procedure ((lambda (x)(x x))5) Can give it a name (define square(lambda(x x x)) (square 5)3 25 (define(fx y z..))is shorthand (define f(lambda(xy z).) (define (square X)C*XX) sept.8,2003 16.410/16.413 Intro. to scheme 5
/ 9 Language elements – Procedural abstraction • use procedures To process something parameters body as a value. Need to capture ways of doing things – multiply it by itself •Special form – creates a procedure and returns it Sept. 8, 2003 16.410 16.413 Intro. to Scheme (lambda (x) (* x x)) Language elements – Procedural abstraction • Use a lambda expression anywhere you would use a procedure ((lambda (x) (* x x)) 5) • Can give it a name – (define square (lambda (x) (* x x))) – (square 5) Æ 25 • (define (f x y z) …)) is shorthand for (define f (lambda (x y z) …)) – (define (square x) (* x x)) Sept. 8, 2003 16.410/16.413 Intro. to Scheme 10 5