The Jo ys of Sc heme Daniel P F riedman Computer - - PDF document

the jo ys of sc heme daniel p f riedman computer science
SMART_READER_LITE
LIVE PREVIEW

The Jo ys of Sc heme Daniel P F riedman Computer - - PDF document

The Jo ys of Sc heme Daniel P F riedman Computer Science Depa rtmert Indiana Universit y XX Conferencia Latinoamericana de Info rmatica P ANEL Mexico Cit y The Jo ys of Sc


slide-1
SLIDE 1
  • The
Jo ys
  • f
Sc heme Daniel P
  • F
riedman Computer Science Depa rtmert Indiana Universit y XX Conferencia Latinoamericana de Info rmatica P ANEL
  • Mexico
Cit y The Jo ys
  • f
Sc heme
slide-2
SLIDE 2
  • Desiderata
  • Keep
it in y
  • ur
head
  • Simple
Syn tax
  • Simple
Seman tics
  • F
reedom
  • f
Expression
  • Tink
erto ys
  • High
P aradigmicit y
  • Dynamically
but Strongly T yp ed
  • W
e shouldnt ha v e to do that
  • Automatic
Memory Managemen t
  • In
teractiv e Program Dev elopmen t The Jo ys
  • f
Sc heme
slide-3
SLIDE 3
  • Outline
  • Ov
erview
  • f
the language
  • Iden
tifying Characteristics
  • Basic
Data T yp es
  • Basic
Con trol Structures
  • Simple
Program St yles
  • Quic
ksort
  • An
illustration
  • f
basic features
  • The
algorithm
  • A
list implemen tation
  • Adv
anced F eatures
  • Syn
tax Extensions
  • FixedP
  • in
ts
  • Another
Example
  • Con
tin uations
  • Milestones
Devils and Angels
  • Using
Con tin uations
  • Exception
Handling
  • Engines
  • Am
b simple v ersion
  • Numeric
Multiple In tegration The Jo ys
  • f
Sc heme
slide-4
SLIDE 4
  • An
Ov erview
  • f
Sc heme The Jo ys
  • f
Sc heme
slide-5
SLIDE 5
  • Iden
tifying Characteristics
  • Ev
erything is data
  • Pro
cedures
  • Con
tin uations
  • All
  • b
jects ha v e indenite exten t
  • Pro
cedures
  • Dynamic
Strong T yping
  • Listbased
syn tax The Jo ys
  • f
Sc heme
slide-6
SLIDE 6
  • Syn
tax expr ession
  • liter
al
  • variable
  • expr
ession
  • lamb
da variable
  • expr
ession
  • set
variable expr ession
  • if
expr ession expr ession expr ession
  • There
are also additional sp e cial forms
  • They
denote
  • ther
language constructs
  • But
are built from this small set The Jo ys
  • f
Sc heme
slide-7
SLIDE 7
  • W
riting Recursiv e Programs dene
  • lamb
da n m cond zero n
  • else
  • sub
n m
  • What
is the value
  • f
  • W
e reason as follo ws
  • Ho
w do w e ll in
  • to
turn
  • into
  • Simple
w e add
  • to
it
  • Lucky
fo r us m is
  • What
is the value
  • f
  • It
must b e the identit y
  • f
  • that
is
  • The
Jo ys
  • f
Sc heme
slide-8
SLIDE 8
  • Multiplication
dene
  • lamb
da n m cond zero n
  • else
  • m
  • sub
n m
  • The
Jo ys
  • f
Sc heme
slide-9
SLIDE 9
  • Y
  • ure
not con vinced dene facto rial lamb da n cond zero n
  • else
  • facto
rial sub n
  • What
is the value
  • f
  • W
e reason as follo ws
  • facto
rial
  • facto
rial
  • Ho
w do w e ll in
  • to
turn
  • into
  • Simple
w e multiply it b y
  • Lucky
fo r us n is
  • What
is the value
  • f
  • It
must b e the identit y
  • f
  • that
is
  • The
Jo ys
  • f
Sc heme
slide-10
SLIDE 10
  • F
actorial dene facto rial lamb da n cond zero n
  • else
  • n
facto rial sub n facto rial
  • The
Jo ys
  • f
Sc heme
slide-11
SLIDE 11
  • Y
  • ure
still not con vinced dene p
  • w
erset lamb da set cond null set
  • else
  • p
  • w
erset cdr set
  • What
is the value
  • f
  • W
e reason as follo ws
  • p
  • w
erset a b c
  • a
b c a b a c a b c b c
  • p
  • w
erset b c
  • b
c b c
  • Ho
w do w e ll in
  • to
turn little set into big set
  • Simple
W e fo rm the union
  • f
adding a to each element
  • f
the little set and the little set itself
  • Lucky
fo r us ca r set is a
  • What
is the value
  • f
  • b
y denition
  • The
Jo ys
  • f
Sc heme
slide-12
SLIDE 12
  • P
  • w
er Set dene p
  • w
erset lamb da set cond null set
  • else
extend ca r set p
  • w
erset cdr set dene extend lamb da item p
  • w
erset app end map lamb da set cons item set p
  • w
erset p
  • w
erset The Jo ys
  • f
Sc heme
slide-13
SLIDE 13
  • Simple
Programs dene facto rialaps lamb da n a cond zero n a else facto rialaps sub n
  • n
a dene facto rial lamb da n facto rialaps n
  • dene
facto rialcps lamb da n k cond zero n k
  • else
facto rialcps sub n lamb da v k
  • v
n dene facto rial lamb da n facto rialcps n lamb da x x The Jo ys
  • f
Sc heme
slide-14
SLIDE 14
  • F
actorial using letrec dene facto rial letrec facto ralaps lamb da n a cond zero n a else facto rialaps sub n
  • n
a lamb da n facto rialaps n
  • dene
facto rial letrec facto rialcps lamb da n k cond zero n k
  • else
facto rialcps sub n lamb da v k
  • n
v lamb da n facto rialcps n lamb da v v The Jo ys
  • f
Sc heme
slide-15
SLIDE 15
  • Basic
Data T yp es A tomic Data T yp es
  • Num
b ers
  • e
  • Characters
n a
  • n
newline
  • Sym
b
  • ls
video cdpla y er radio Comp
  • und
Ob jects
  • Lists
Constructors
  • cons
list AccessorsMutators ca r cdr setca r setcdr Predicates null pair list The Jo ys
  • f
Sc heme
slide-16
SLIDE 16
  • Basic
Data T yp es
  • V
ectors Constructor vecto r AccessorMutator vecto rref vecto rset Predicate vecto r
  • Strings
Constructor string AccessorMutator stringref stringset Predicate string The Jo ys
  • f
Sc heme
slide-17
SLIDE 17
  • Basic
Data T yp es Unprin table Data T yp es
  • Pro
cedures Constructor lamb da formals b
  • dy
  • Accessor
  • p
er ator
  • p
er ands
  • Application
Predicate p ro cedure
  • Con
tin uations Constructor callcc Accessor Applic ation Predicate p ro cedure The Jo ys
  • f
Sc heme
slide-18
SLIDE 18
  • Basic
Con trol Structures
  • Conditionals
  • if
testexp thenexp elseexp
  • cond
testexp exp
  • Sequencing
b egin E
  • E
n
  • Lo
  • ping
and Iteration By Recursion The Jo ys
  • f
Sc heme
slide-19
SLIDE 19
  • An
Illustration Quic ksort Problem Sort a giv en set sa y X
  • f
n um b ers in ascend ing
  • rder
F
  • r
example
  • Boundary
Condition
  • If
empt y set sorted v ersion is the empt y sequence Step
  • Cho
  • se
a piv
  • t
n st n
  • X
  • Step
  • P
artition the list hA B i st A
  • fx
  • X
j x
  • ng
and B
  • fx
  • X
j x
  • ng
  • Step
  • Sort
partitions and join them A
  • n
  • B
  • where
A
  • and
B
  • are
sorted v ersions
  • f
A and B
  • The
Jo ys
  • f
Sc heme
slide-20
SLIDE 20
  • An
Illustration Quic ksort Represen t sets as lists dene quickso rt lamb da ls cond null ls
  • else
let pivot ca r ls app end quickso rt lo w erpa rtition pivot cdr ls list pivot quickso rt upp erpa rtition pivot cdr ls
  • cond
is a conditional
  • let
is a binding construct
  • app
end joins t w
  • lists
The Jo ys
  • f
Sc heme
slide-21
SLIDE 21
  • An
Illustration Quic ksort dene lo w erpa rtition lamb da pivot ls lterin lamb da x
  • pivot
x ls dene upp erpa rtition lamb da pivot ls lterin lamb da x
  • pivot
x ls dene lterin lamb da test ls cond null ls
  • test
ca r ls cons ca r ls lterin test cdr ls else lterin test cdr ls The Jo ys
  • f
Sc heme
slide-22
SLIDE 22
  • Using
Con tin uation P assing St yle dene quickso rt lamb da ls cond null ls
  • else
let pivot ca r ls pa rtition pivot cdr ls lamb da lo w erpa rtition upp erpa rtition app end quickso rt lo w erpa rtition list pivot quickso rt upp erpa rtition The Jo ys
  • f
Sc heme
slide-23
SLIDE 23
  • Using
Con tin uation P assing St yle dene pa rtition lamb da pivot ls k cond null ls k
  • else
pa rtition pivot cdr ls lamb da lo w erpa rtition upp erpa rtition if
  • ca
r ls pivot k cons ca r ls lo w erpa rtition upp erpa rtition k lo w erpa rtition cons ca r ls upp erpa rtition The Jo ys
  • f
Sc heme
slide-24
SLIDE 24
  • Seman
tics
  • f
some sp ecial forms
  • In
terms
  • f
the basic syn tax let v e
  • b
  • dy
b
  • dy
  • lamb
da v
  • b
  • dy
b
  • dy
  • e
  • blo
ck v
  • b
  • dy
b
  • dy
  • let
v
  • b
  • dy
b
  • dy
  • letrec
v e
  • b
  • dy
b
  • dy
  • blo
ck v
  • set
v e
  • b
  • dy
b
  • dy
  • b
egin e e
  • lamb
da
  • e
e
  • while
testexp e e
  • letrec
lo
  • p
lamb da
  • if
testexp b egin e e
  • lo
  • p
lo
  • p
The Jo ys
  • f
Sc heme
slide-25
SLIDE 25
  • FixedP
  • in
ts
  • Another
Example Fixedp
  • in
t algorithms are fairly common
  • Numerical
Algorithms
  • Set
Equations
  • Program
Analyses etc Rep eat a certain computation un til c hanges in the so lution are within a certain b
  • und
F
  • r
example let us compute the v alue
  • f
  • tan
  • tan
  • dene
pi letrecequations x
  • x
  • expt
  • n
  • add
  • n
  • n
  • add
n lamb da x y t
  • x
The Jo ys
  • f
Sc heme
slide-26
SLIDE 26
  • FixedP
  • in
ts
  • Another
Example In suc h cases
  • ne
usually b egins with an initial solution and then iterates
  • letrecequations
is a sp ecial form
  • It
is not in standard Sc heme
  • It
is a great abstraction for a fairly common st yle
  • f
programming
  • Ho
w general is this abstraction The Jo ys
  • f
Sc heme
slide-27
SLIDE 27
  • FixedP
  • in
ts
  • Another
Example Set equations are useful in man y con texts F
  • r
example The Philosophers P art y
  • A
philosopher will attend the part y i his friends will
  • Find
a suitable guestlist dene guestlist lamb da inits letrecequations guests inits union guests friends guests
  • guests
guestlist a ristotle
  • The
Jo ys
  • f
Sc heme
slide-28
SLIDE 28
  • FixedP
  • in
ts
  • Another
Example
  • W
e can add letrecequations to Sc heme
  • Macros
  • r
syn tax extensions allo w syn tactic forms to b e added to the language
  • Macros
are not y et a part
  • f
standard Sc heme but will so
  • n
b e The Jo ys
  • f
Sc heme
slide-29
SLIDE 29
  • FixedP
  • in
ts
  • Another
Example letrecequations va r init exp bina rytest
  • b
  • dy
b
  • dy
  • letrec
lo
  • p
lamb da va r
  • if
and bina rytest va r exp
  • b
egin b
  • dy
b
  • dy
  • lo
  • p
exp
  • lo
  • p
init
  • The
Jo ys
  • f
Sc heme
slide-30
SLIDE 30
  • FixedP
  • in
ts
  • Another
Example dene friends let lo
  • kuplist
a ristotle plato alexander plato so crates so crates alexander ptolemy homer virgil sopho cles virgil dante ho race dante p etrach p etrach sidney shak esp ea re lamb da guests mapunion lamb da g let ans assq g lo
  • kuplist
if ans cdr ans
  • guests
The Jo ys
  • f
Sc heme
slide-31
SLIDE 31
  • Con
tin uations
  • A
generalized Goto
  • A
con tin uation is a pro cedure that represen ts the rest
  • f
the computation
  • A
con tin uation can b e captured
  • If
a con tin uation is restored it resets the computation bac k to the p
  • in
t the con tin uation w as captured
  • Primary
Uses
  • Nonstandard
con trol constructs coroutines
  • Exception
Handling
  • Bac
ktrac king as in prolog The Jo ys
  • f
Sc heme
slide-32
SLIDE 32
  • Escaping
Pro cedures Pro cedures that ab
  • rt
when applied Examples
  • v
  • v
  • v
  • v
  • f
  • v
  • v
  • F
  • r
any f
  • f
  • v
g
  • v
  • g
  • The
Jo ys
  • f
Sc heme
slide-33
SLIDE 33
  • CallccBasics
cons
  • callcc
lamb da k k cons
  • k
v v callcc lamb da k k cons
  • k
v v
  • cons
callcc lamb da k k
  • k
v cons v
  • The
Jo ys
  • f
Sc heme
slide-34
SLIDE 34
  • CallccBasics
cons
  • callcc
lamb da k k
  • k
v cons
  • v
cons
  • callcc
lamb da k k
  • k
v cons
  • v
  • cons
  • callcc
lamb da k k
  • k
v cons
  • v
  • The
Jo ys
  • f
Sc heme
slide-35
SLIDE 35
  • Milestones
Devils and Angels Milestone An imp
  • rtan
t ev en tw
  • rth
getting bac k to Devil T ak es y
  • u
bac k to y
  • ur
last milestone in exc hange for y
  • ur
soul Angel Undo es the w
  • rk
  • f
the devil
  • The
devils job is to k eep the computation from hap p ening Errors and exceptions
  • A
milestone is a place where the computation go es bac k to reco v er
  • An
angel causes the computation to reco v er The Jo ys
  • f
Sc heme
slide-36
SLIDE 36
  • Milestones
Devils and Angels dene mda lamb da
  • cond
  • a
  • b
milestone
  • b
egin writeln here
  • c
angel allisw ell
  • d
  • e
milestone
  • f
angel
  • g
devil
  • else
b egin writeln made it to
  • h
devil
  • In
b
  • In
a
  • f
In e
  • In
d
  • f
made it to In e
  • In
d
  • t
In b
  • In
a
  • t
here In g
  • allisw
ell In h
  • allisw
ell allisw ell The Jo ys
  • f
Sc heme
slide-37
SLIDE 37
  • Milestones
Devils and Angels dene past
  • dene
future
  • push
s v
  • set
s cons v s p
  • p
s
  • if
null s erro r Cannot p
  • p
empt y stack let v ca r s set s cdr s v The Jo ys
  • f
Sc heme
slide-38
SLIDE 38
  • Milestones
Devils and Angels dene milestone lamb da v callcc lamb da k push past k v dene devil lamb da v callcc lamb da k push future k p
  • p
past v dene angel lamb da v p
  • p
future v The Jo ys
  • f
Sc heme
slide-39
SLIDE 39
  • Using
Con tin uationsException Handling W e w an t to con trol ho w errors are handled dene div lamb da x if
  • x
  • raise
divideb yzero
  • x
dene fo
  • lamb
da n div n dene test withhandler divideb yzero lamb da exn f
  • fo
  • f
  • raise
raises the exception
  • withhandler
traps it The Jo ys
  • f
Sc heme
slide-40
SLIDE 40
  • Using
Con tin uationsException Handling Handlers ha v e uid sc
  • p
e ie the last handler established at the time the error
  • ccured
m ust b e c hosen
  • Stac
k
  • f
handlers
  • Handlers
should kno w where to resume execution dene handlers
  • withhandler
name handler b
  • dy
b
  • dy
  • callcc
lamb da k uidlet handlers cons list name k handler handlers b
  • dy
b
  • dy
  • The
Jo ys
  • f
Sc heme
slide-41
SLIDE 41
  • Using
Con tin uationsException Handling dene raise lamb da exn let res assq exn handlers cond pair res apply lamb da name k handler k handler exn res else erro r Exception is not handled exn The Jo ys
  • f
Sc heme
slide-42
SLIDE 42
  • EnginesAm
b Simple V ersion Engines An abstraction
  • f
async hronous con trol Am b Return the v alue
  • f
the expression that returns rst
  • am
b factorial
  • factorial
  • am
b factorial
  • factorial
  • am
b factorial
  • factorial
  • am
b factorial
  • factorial
  • am
b factorial
  • factorial
  • The
Jo ys
  • f
Sc heme
slide-43
SLIDE 43
  • EnginesAm
b Simple V ersion
  • aneng
ticks success failure
  • ticks
is an integer
  • success
is a a rgument p ro cedure
  • Its
rst a rgument is the value
  • f
the
  • riginal
a rgument to engine
  • Its
second a rgument is the unused ticks
  • failure
is a a rgument p ro cedure
  • Its
  • nly
a rgument is a new engine
  • If
it is invok ed it sta rts where the
  • ld
  • ne
quit The Jo ys
  • f
Sc heme
slide-44
SLIDE 44
  • EnginesAm
b Simple V ersion amb e e
  • ambengines
engine e engine e dene ambengines lamb da engine engine engine random
  • cons
lamb da engine engine random
  • cons
lamb da engine ambengines engine engine What if cons is replaced b y lamb da value ticks value
  • What
if cons is replaced b y lamb da value ticks ticks
  • The
Jo ys
  • f
Sc heme
slide-45
SLIDE 45
  • Gaussian
Quadrature
  • p
  • in
t Gaussian Quadrature algorithm for nding denite in tegrals
  • The
algorithm is exact to the th degree mo dulo the inexactness
  • f
the co ecien ts used
  • Sc
hemes elegance is brough t
  • ut
as usual b y lamb da
  • The
follo wing algorithm computes denite in tegrals
  • v
er the in terv al
  • so
in general a trans lation
  • f
the in terv al is needed
  • An
y
  • rdinary
language lik e C
  • r
P ascal w
  • uld
require us to dene a help function dened
  • n
the in terv al
  • but
with Sc hemes supp
  • rt
  • f
anon ymous pro cedures via lamb da this isnt necessary
  • The
substitution is done
  • n
the y The Jo ys
  • f
Sc heme
slide-46
SLIDE 46
  • Gaussian
Quadrature p
  • int
Gaussian quadrature Exact to the
  • th
degree dene int let ro
  • ts
  • ro
  • ts
  • f
  • th
Legendre p
  • ly
  • co
es
  • co
es fo r its resp ective ro
  • t
  • The
Jo ys
  • f
Sc heme
slide-47
SLIDE 47
  • Gaussian
Quadrature let intfromnegto lamb da f
  • letrec
lo
  • p
lamb da co es ro
  • ts
cond null co es
  • else
  • ca
r co es f ca r ro
  • ts
lo
  • p
cdr co es cdr ro
  • ts
lo
  • p
co es ro
  • ts
lamb da f limitpair intfromnegto translate f limitpair cons
  • The
Jo ys
  • f
Sc heme
slide-48
SLIDE 48
  • Gaussian
Quadrature dene translate lamb da f limitpair limitpair let from ca r limitpair to cdr limitpair from ca r limitpair to cdr limitpair let m
  • to
from
  • to
from lamb da x
  • m
f
  • m
  • x
from from The Jo ys
  • f
Sc heme
slide-49
SLIDE 49
  • Gaussian
Quadrature R
  • x
  • dx
  • int
lamb da x
  • x
x cons
  • T
  • compute
a double in tegral w e need to mak e sure w e get the v ariables assigned correctly R
  • R
  • xy
dydx
  • int
lamb da x int lamb da y
  • x
y cons
  • cons
  • The
Jo ys
  • f
Sc heme
slide-50
SLIDE 50
  • Gaussian
Quadrature But with Sc hemes abilit y to curry s w e can surely do b etter
  • dene
f lamb da x lamb da y
  • x
y
  • int
lamb da x int lamb da y f x y cons
  • cons
  • W
e can no w dene doubleint as follo ws dene doubleint lamb da f limitpair limitpair int lamb da x int lamb da y f x y limitpair limitpair The Jo ys
  • f
Sc heme
slide-51
SLIDE 51
  • Gaussian
Quadrature
  • doubleint
lamb da x lamb da y
  • x
y
  • W
e can write an integrato r that tak es multiple integrations dene nint lamb da f limitpairs cond null limitpairs f
  • else
int lamb da x nint f x cdr limitpairs ca r limitpairs
  • nint
lamb da x lamb da y
  • x
y
  • The
Jo ys
  • f
Sc heme
slide-52
SLIDE 52
  • Gaussian
Quadrature W e can abstract
  • ver
int allo wing any integrato r dene nintmak er lamb da int letrec nint lamb da f limitpairs cond null limitpairs f
  • else
int lamb da x nint f x cdr limitpairs ca r limitpairs nint dene gaussianquadraturenint nintmak er int The Jo ys
  • f
Sc heme
slide-53
SLIDE 53
  • Gaussian
Quadrature Or w e can asso ciate a dierent integration metho d with each limit pair b y fo rming a new data structure that has b
  • th
the integrato r and its limit pair dene sup ernint lamb da f intlimitspairs cond null intlimitspairs f
  • else
ca r ca r intlimitspairs lamb da x sup ernint f x cdr intlimitspairs cdr ca r intlimitspairs
  • sup
ernint lamb da x lamb da y
  • x
y list cons int
  • cons
int
  • The
Jo ys
  • f
Sc heme
slide-54
SLIDE 54
  • The
Simplicit y
  • f
Sc heme
  • va
riable reference lamb da application
  • set
  • if
  • callcc
  • engines
  • Primitiv
es
  • n
data structures
  • dene
  • The
Jo ys
  • f
Sc heme
slide-55
SLIDE 55
  • Wh
y Sc heme
  • P
aradigmatic Considerations
  • Simplicit
y
  • Minimalit
y
  • Flexibilit
y
  • Wide
range
  • f
concepts can b e realized with just a few constructs
  • T
ec hnical Considerations
  • In
teractiv e Programming
  • Automatic
Memory Managemen t and T yp e Safet y
  • Implemen
tations
  • n
a wide v ariet y
  • f
platforms
  • Wh
y not The Jo ys
  • f
Sc heme