Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig
Compiler Implementation Source Code Compiler Object Code Linker Executable code Object Modules
Interpreter Implementation Source Code Interpreter Code Input - Source code is data for the interpreter - Interpreter code is executed directly on the machine
Dr. Scheme Available at Scheme interpreter Select Language, Choose Language, How to Design Programs, Essentials of Programming Languages (2 nd Ed.) Screen divided into 2 areas
Introduction to Scheme Data values are allocated on the heap Objects are “first class” – can be passed in and out of procedures Variables are lexically scoped (define x 3) (let ((x 2) (y 5)) (+ x y))
Introduction to Scheme Programs are “block structured” (define x (lambda (x) (+ x x))) Blocks can be nested (let ((x 2) (y 5)) (let ((x 6)) (+ x y)))
Introduction to Scheme Procedures can be defined at the top level, or within another block (define incr (lambda (a) (+ a 1))) (let ((incr (lambda (a) (+ a 1))) (x 3)) (incr x))
Introduction to Scheme Procedures are first class objects (define square (lambda (x) (* x x))) (define double (lambda (x) (+ x x))) (define apply (lambda (fn p) (fn p))) (apply square 3) (apply double 4)
Introduction to Scheme Procedures can be unnamed ((lambda (x y)(+ x y)) 3 4) Procedures can be named (define add (lambda (x y)(+ x y))) (add 3 4)
Introduction to Scheme Scheme supports recursive functions (define factorial (lambda (x) (cond ((= x 1) 1) (else (* x (factorial (- x 1))))))) (factorial 100)
Scheme Syntax Scheme programs are made up of keywords, variables, structured forms, constant data (numbers, characters, strings, quoted vectors, quoted lists, quoted symbols, etc.), whitespace, and comments.
Scheme Syntax Keywords, variables, and symbols are collectively called identifiers. Identifiers may be formed from the following set of characters: a – z, A – Z, 0 – 9, ? !. + - * / : $ % ^ & _ ~ Identifiers normally cannot start with any character that may start a number
Scheme Syntax No inherent limit on the length of a Scheme identifier Structured forms and list constants are enclosed within parentheses, e.g., (a b c) or (* (- x 2) y). The empty list is written () Boolean values representing true and false are written as #t and #f
Scheme Syntax Vectors are preceded by #( and terminated by ). #(a b c d) Strings are enclosed in double quotation marks. " This is a string" Characters are preceded by #\. #\a. Numbers may be written as integers, ratios, floating-point,or scientific notation, or as complex numbers in rectangular or polar notation, 123, ½, 1e23,
Scheme Syntax Comments appear between a semicolon ( ; ) and the end of the line.
Scheme Naming Conventions Predicate names end in a question mark ( ? ). eq?, zero?, and string=?. Common numeric comparators =,, = are exceptions to this rule Most character, string, and vector procedure names start with the prefix char-, string-, and vector-, e.g., string- append
Scheme Naming Conventions Names of procedures that convert an object of one type into an object of another type are written as type1- >type2, e.g., vector->list. Names of procedures and syntactic forms that cause side effects end with an exclamation point ( ! ). set! and vector-set!