Introduction To Scheme: Slides Created by Marty Stepp
Introduction To Scheme: Slides Created by Marty Stepp
Lecture 15
introduction to Scheme
slides created by Marty Stepp
http://www.cs.washington.edu/341/
Looking back: Language timeline
category 1960s 1970s 1980s 1990s 2000s
scientific Fortran Matlab
business Cobol DBMSes SQL VB
functional Lisp ML, Scheme Erlang Haskell F#
imperative/ Algol Pascal, C, Ada, C++ Java C#
procedural Smalltalk
scripting BASIC Perl Python,
Ruby, PHP,
JavaScript
logical Prolog CLP(R)
2
History of LISP
• LISP ("List Processing"): The first functional language.
made: 1958 by John McCarthy, MIT (Turing Award winner)
– godfather of AI (coined the term "AI")
developed as a math notation for proofs about programs
pioneered idea of a program as a collection of functions
became language of choice for AI programming
4
LISP key features
• a functional, dynamically typed, type-safe, language
anonymous functions, closures, no return statement, etc.
less compile-time checking (run-time checking instead)
accepts more programs that ML would reject
http://www.teach-scheme.org/
http://www.htdp.org/
9
DrScheme
• DrScheme: an educational editor for Scheme programs
built-in interpreter window
– Alt+P, Alt+N = history
syntax highlighting
graphical debugger
multiple "language levels"
– (set ours to "Pretty Big")
10
Scheme data types
• numbers
integers: 42 -15
rational numbers: 1/3 -3/5
real numbers: 3.14 .75 2.1e6
complex/imaginary: 3+2i 0+4i
• text
strings: "\"Hello\", I said!"
characters: #\X #\q
• boolean logic: #t #f
• lists and pairs: (a b c) '(1 2 3) (a . b)
• symbols: x hello R2D2 u+me
11
Basic arithmetic procedures
(procedure arg1 arg2 ... argN)
• Examples:
(+ 2 3) → 5 ; 2 + 3
(- 9 (+ 3 4)) → 2 ; 9 - (3 + 4)
(* 6 -7) → -42 ; 6 * -7
(/ 32 6) → 16/3 ; 32/6 (rational)
(/ 32.0 6) → 5.333... ; real number
(- (/ 32 6) (/ 1 3)) → 5 ; 32/6 - 1/3 (int)
12
More arithmetic procedures
+ - *
quotient remainder modulo
max min abs
numerator denominator gcd
lcm floor ceiling
truncate round rationalize
expt
• Examples:
(define x 3) ; int x = 5;
(define y (+ 2 x)) ; int y = 2 + x;
(define z (max y 7 3)) ; int z = Math.max..
15
Basic logic
• #t, #f ; atoms for true/false
• Examples:
> (define x 10)
> (if (< x 3) 10 25)
25
> (if (> x 6) (* 2 4) (+ 1 2))
8
> (if (> 0 x) 42 (if (< x 100) 999 777)) ; nested if
999
17
The cond expression
(cond (test1 expr1) (test2 expr2)
... (testN exprN))
• solution:
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
20
List of Scheme keywords
=> do or
and else quasiquote
begin if quote
case lambda set!
cond let unquote
define let* unquote-splicing
delay letrec
21