17
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Embed Size (px)

Citation preview

Page 1: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Introduction to Scheme

Lectures on The Scheme Programming

Language, 2nd Ed.R. Kent Dybvig

Page 2: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Compiler Implementation

Source

CodeCompiler Object

Code

LinkerExecutable code

Object Modules

Page 3: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Interpreter Implementation

Source Code

Interpreter CodeInput

- Source code is data for the interpreter

- Interpreter code is executed directly on the machine

Page 4: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Dr. Scheme

Available at http://www.drscheme.org/

Scheme interpreter Select Language, Choose Language,

How to Design Programs, Essentials of Programming Languages (2nd Ed.)

Screen divided into 2 areas

Page 5: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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))

Page 6: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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)))

Page 7: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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))

Page 8: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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)

Page 9: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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)

Page 10: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Introduction to Scheme

Scheme supports recursive functions

(define factorial (lambda (x) (cond ((= x 1) 1) (else (* x (factorial (- x 1)))))))(factorial 100)

Page 11: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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.

Page 12: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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

Page 13: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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

Page 14: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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, 1.5+2.2i,-3,4@45

Page 15: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Scheme Syntax

Comments appear between a semicolon ( ; ) and the end of the line.

Page 16: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

Scheme Naming Conventions Predicate names end in a question

mark ( ? ). eq?, zero?, and string=?. Common numeric comparators =,

<, >, <=, and >= are exceptions to this rule

Most character, string, and vector procedure names start with the prefix char-, string-, and vector-, e.g., string-append

Page 17: Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig

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!