55
8/14/2019 The Joys of Scheme http://slidepdf.com/reader/full/the-joys-of-scheme 1/55

The Joys of Scheme

Embed Size (px)

Citation preview

Page 1: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 1/55

'  

&  

   $

   %

T h e J o y s o f S c h e m e    

D a n i e l P . F r i e d m a n    

C o m p u t e r S c i e n c e D e p a r t m e r t    

I n d i a n a U n i v e r s i t y    

T h e J o y s o f S c h e m e : 1 ]  

Page 2: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 2/55

'  

&  

   $

   %

D e s i d e r a t a      

  K e e p i t i n y o u r h e a d      

{   S i m p l e S y n t a x .  

{   S i m p l e S e m a n t i c s .  

  F r e e d o m o f E x p r e s s i o n      

{   T i n k e r t o y s    

{   H i g h P a r a d i g m i c i t y .  

{   D y n a m i c a l l y ( b u t S t r o n g l y ) T y p e d .  

  W e s h o u l d n ' t h a v e t o d o t h a t !  

{   A u t o m a t i c M e m o r y M a n a g e m e n t    

{   I n t e r a c t i v e P r o g r a m D e v e l o p m e n t    

T h e J o y s o f S c h e m e : 2 ]  

Page 3: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 3/55

'  

&  

   $

   %

O u t l i n e    

  O v e r v i e w o f t h e l a n g u a g e    

{   I d e n t i f y i n g C h a r a c t e r i s t i c s    

{   B a s i c D a t a T y p e s    

{   B a s i c C o n t r o l S t r u c t u r e s    

  S i m p l e P r o g r a m S t y l e s    

  Q u i c k s o r t { A n i l l u s t r a t i o n o f b a s i c f e a t u r e s    

{   T h e a l g o r i t h m        

{   A l i s t i m p l e m e n t a t i o n      

  A d v a n c e d F e a t u r e s    

{   S y n t a x E x t e n s i o n s    

{   F i x e d - P o i n t s { A n o t h e r E x a m p l e    

{   C o n t i n u a t i o n s { M i l e s t o n e s , D e v i l s a n d A n g e l s    

{   U s i n g C o n t i n u a t i o n s { E x c e p t i o n H a n d l i n g      

{   E n g i n e s { A m b ( s i m p l e v e r s i o n )    

{   N u m e r i c M u l t i p l e I n t e g r a t i o n      

T h e J o y s o f S c h e m e : 3 ]  

Page 4: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 4/55

'  

&  

   $

   %

A n O v e r v i e w o f S c h e m e    

T h e J o y s o f S c h e m e : 4 ]  

Page 5: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 5/55

'  

&  

   $

   %

I d e n t i f y i n g C h a r a c t e r i s t i c s    

  E v e r y t h i n g i s d a t a .  

{   P r o c e d u r e s    

{   C o n t i n u a t i o n s    

  A l l o b j e c t s h a v e i n d e n i t e e x t e n t .  

{   P r o c e d u r e s .  

  D y n a m i c , S t r o n g T y p i n g      

  L i s t - b a s e d s y n t a x .  

T h e J o y s o f S c h e m e : 5 ]  

Page 6: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 6/55

'  

&  

   $

   %

S y n t a x      

e x p r e s s i o n      !     

l i t e r a l    

!     v a r i a b l e      

!     ( e x p r e s s i o n      : : : 

)  

!      (   l a m b d a    ( v a r i a b l e      : : :  ) e x p r e s s i o n      : : : 

)  

!      (   s e t !   v a r i a b l e e x p r e s s i o n )    

!      (   i f   e x p r e s s i o n e x p r e s s i o n e x p r e s s i o n )    

  T h e r e a r e a l s o a d d i t i o n a l    s p e c i a l f o r m s    

  T h e y d e n o t e o t h e r l a n g u a g e c o n s t r u c t s .  

  B u t a r e b u i l t f r o m t h i s s m a l l s e t .  

T h e J o y s o f S c h e m e : 6 ]  

Page 7: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 7/55

'  

&  

   $

   %

W r i t i n g R e c u r s i v e P r o g r a m s    

( d e n e    * 

( l a m b d a ( n m )    

( c o n d    

( ( z e r o ? n ) ? )    

( e l s e ( ? ? (    *  ( s u b 1 n ) m ) ) ) ) ) )    

  W h a t i s t h e v a l u e o f ? ?    

  W e r e a s o n a s f o l l o w s :  

   (  *  5 8 )  

!     4 0  

   (  *  4 8 )  

!     3 2  

  H o w d o w e l l i n ? ? t o t u r n 3 2 i n t o 4 0 ?    

  S i m p l e , w e a d d 8 t o i t .  

  L u c k y f o r u s , m i s 8 .  

  W h a t i s t h e v a l u e o f ?    

  I t m u s t b e t h e i d e n t i t y o f + ( t h a t i s 0 ) .  

T h e J o y s o f S c h e m e : 7 ]  

Page 8: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 8/55

'  

&  

   $

   %

M u l t i p l i c a t i o n      

( d e n e    * 

( l a m b d a ( n m )    

( c o n d    

( ( z e r o ? n ) 0 )    

( e l s e ( + m (    *  ( s u b 1 n ) m ) ) ) ) ) )    

(  *  5 8 )  

!     4 0  

T h e J o y s o f S c h e m e : 8 ]  

Page 9: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 9/55

'  

&  

   $

   %

Y o u ' r e n o t c o n v i n c e d ?    

( d e n e f a c t o r i a l  

( l a m b d a ( n )    

( c o n d    

( ( z e r o ? n ) ? )    

( e l s e ( ? ? ( f a c t o r i a l ( s u b 1 n ) ) ) ) ) ) )    

  W h a t i s t h e v a l u e o f ? ?    

  W e r e a s o n a s f o l l o w s :  

  ( f a c t o r i a l 5 )    

!     1 2 0  

  ( f a c t o r i a l 4 )    

!     2 4   

  H o w d o w e l l i n ? ? t o t u r n 2 4 i n t o 1 2 0 ?    

  S i m p l e , w e m u l t i p l y i t b y 5 .  

  L u c k y f o r u s , n i s 5 .  

  W h a t i s t h e v a l u e o f ?    

  I t m u s t b e t h e i d e n t i t y o f    

( t h a t i s 1 )    

T h e J o y s o f S c h e m e : 9 ]  

Page 10: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 10/55

'  

&  

   $

   %

F a c t o r i a l    

( d e n e f a c t o r i a l  

( l a m b d a ( n )    

( c o n d    

( ( z e r o ? n ) 1 )    

( e l s e (    *  n ( f a c t o r i a l ( s u b 1 n ) ) ) ) ) ) )    

( f a c t o r i a l 5 )    !     

1 2 0  

T h e J o y s o f S c h e m e : 1 0 ]  

Page 11: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 11/55

'  

&  

   $

   %

Y o u ' r e s t i l l n o t c o n v i n c e d ?    

( d e n e p o w e r - s e t    

( l a m b d a ( s e t )    

( c o n d    

( ( n u l l ? s e t ) ? )    

( e l s e ( ? ? ( p o w e r - s e t ( c d r s e t ) ) ) ) ) ) )    

  W h a t i s t h e v a l u e o f ? ?    

  W e r e a s o n a s f o l l o w s :  

  ( p o w e r - s e t ' ( a b c ) )    

!     

( ( a b c ) ( a b ) ( a c ) ( a ) ( b c ) ( b ) ( c ) ( ) )    

  ( p o w e r - s e t ' ( b c ) )    

!     ( ( b c ) ( b ) ( c ) ( ) )    

  H o w d o w e l l i n ? ? t o t u r n \ l i t t l e s e t " i n t o \ b i g s e t " ?    

  S i m p l e , W e f o r m t h e u n i o n o f a d d i n g a t o e a c h e l e m e n t    

o f t h e l i t t l e s e t a n d t h e l i t t l e s e t i t s e l f .  

  L u c k y f o r u s , ( c a r s e t ) i s a .  

  W h a t i s t h e v a l u e o f ? : b y d e n i t i o n ( ( ) ) .  

T h e J o y s o f S c h e m e : 1 1 ]  

Page 12: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 12/55

'  

&  

   $

   %

P o w e r S e t    

( d e n e p o w e r - s e t    

( l a m b d a ( s e t )    

( c o n d    

( ( n u l l ? s e t ) ' ( ( ) ) )    

( e l s e ( e x t e n d ( c a r s e t ) ( p o w e r - s e t ( c d r s e t ) ) ) ) ) ) )    

( d e n e e x t e n d    

( l a m b d a ( i t e m p o w e r - s e t )    

( a p p e n d    

( m a p ( l a m b d a ( s e t ) ( c o n s i t e m s e t ) ) p o w e r - s e t )    

p o w e r - s e t ) ) )    

T h e J o y s o f S c h e m e : 1 2 ]  

Page 13: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 13/55

'  

&  

   $

   %

S i m p l e P r o g r a m s    

( d e n e f a c t o r i a l - a p s    

( l a m b d a ( n a )    

( c o n d    

( ( z e r o ? n ) a )    

( e l s e ( f a c t o r i a l - a p s ( s u b 1 n ) (    *  n a ) ) ) ) ) )    

( d e n e f a c t o r i a l  

( l a m b d a ( n )    

( f a c t o r i a l - a p s n 1 ) )    

( d e n e f a c t o r i a l - c p s    

( l a m b d a ( n k )    

( c o n d    

( ( z e r o ? n ) ( k 1 ) )    

( e l s e ( f a c t o r i a l - c p s ( s u b 1 n )    

( l a m b d a ( v ) ( k (    *  v n ) ) ) ) ) ) ) )    

( d e n e f a c t o r i a l  

( l a m b d a ( n )    

( f a c t o r i a l - c p s n ( l a m b d a ( x ) x ) ) ) )    

T h e J o y s o f S c h e m e : 1 3 ]  

Page 14: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 14/55

'  

&  

   $

   %

F a c t o r i a l u s i n g l e t r e c    

( d e n e f a c t o r i a l  

( l e t r e c    

( ( f a c t o r a l - a p s    

( l a m b d a ( n a )    

( c o n d    

( ( z e r o ? n ) a )    

( e l s e ( f a c t o r i a l - a p s ( s u b 1 n ) (    * 

n a ) ) ) ) ) ) )    

( l a m b d a ( n )    

( f a c t o r i a l - a p s n 1 ) ) ) )    

( d e n e f a c t o r i a l  

( l e t r e c    

( ( f a c t o r i a l - c p s    

( l a m b d a ( n k )    

( c o n d    

( ( z e r o ? n ) ( k 1 ) )    

( e l s e ( f a c t o r i a l - c p s ( s u b 1 n )    

( l a m b d a ( v ) ( k (    *  n v ) ) ) ) ) ) ) ) )    

( l a m b d a ( n )    

( f a c t o r i a l - c p s n ( l a m b d a ( v ) v ) ) ) ) )    

T h e J o y s o f S c h e m e : 1 4 ]  

Page 15: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 15/55

'  

&  

   $

   %

B a s i c D a t a T y p e s    

A t o m i c D a t a T y p e s    

  N u m b e r s :   9 , 1 0 . 9 , 3 3 e 1 0    

  C h a r a c t e r s :  

#     n  a , #    

n  n e w l i n e    

  S y m b o l s :   v i d e o , c d - p l a y e r , r a d i o ?    

C o m p o u n d O b j e c t s    

  L i s t s :  

C o n s t r u c t o r s :    ' ( ) , c o n s , l i s t    

A c c e s s o r s / M u t a t o r s :    c a r , c d r , s e t - c a r ! , s e t - c d r !  

P r e d i c a t e s :    n u l l ? , p a i r ? , l i s t ?    

T h e J o y s o f S c h e m e : 1 5 ]  

Page 16: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 16/55

'  

&  

   $

   %

B a s i c D a t a T y p e s  

  V e c t o r s :  

C o n s t r u c t o r :    v e c t o r    

A c c e s s o r / M u t a t o r :    v e c t o r - r e f , v e c t o r - s e t !  

P r e d i c a t e :    v e c t o r ?    

  S t r i n g s :  

C o n s t r u c t o r :    s t r i n g      

A c c e s s o r / M u t a t o r :    s t r i n g - r e f , s t r i n g - s e t !  

P r e d i c a t e :    s t r i n g ?    

T h e J o y s o f S c h e m e : 1 6 ]  

Page 17: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 17/55

'  

&  

   $

   %

B a s i c D a t a T y p e s  

U n p r i n t a b l e D a t a T y p e s    

  P r o c e d u r e s    

C o n s t r u c t o r :    ( l a m b d a     f o r m a l s b o d y      )  

A c c e s s o r :    (  

o p e r a t o r o p e r a n d s    : : : )  

( A p p l i c a t i o n ) .  

P r e d i c a t e :    p r o c e d u r e ?    

  C o n t i n u a t i o n s    

C o n s t r u c t o r :    c a l l / c c    

A c c e s s o r :    A p p l i c a t i o n      

P r e d i c a t e :    p r o c e d u r e ?    

T h e J o y s o f S c h e m e : 1 7 ]  

Page 18: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 18/55

'  

&  

   $

   %

B a s i c C o n t r o l S t r u c t u r e s    

  C o n d i t i o n a l s :  

{   ( i f   t e s t - e x p t h e n - e x p e l s e - e x p      

)  

{   ( c o n d (    t e s t - e x p e x p      

)  : : : 

)  

  S e q u e n c i n g :   ( b e g i n    

E    

: : : E        

)  

  L o o p i n g a n d I t e r a t i o n : B y R e c u r s i o n .  

T h e J o y s o f S c h e m e : 1 8 ]  

Page 19: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 19/55

'  

&  

   $

   %

A n I l l u s t r a t i o n : Q u i c k s o r t    

P r o b l e m : S o r t a g i v e n s e t , s a y      X    

, o f n u m b e r s i n a s c e n d -    

i n g o r d e r . F o r e x a m p l e :  

7 3 8 9 1    

B o u n d a r y C o n d i t i o n      : I f e m p t y s e t , s o r t e d v e r s i o n i s    

t h e e m p t y s e q u e n c e .  

S t e p 1      C h o o s e a p i v o t :  n   

s . t .  n    2    X     . 

7 3 8 9 1    

S t e p 2      P a r t i t i o n t h e l i s t :  h  

A ; B    i  

s . t .  

A     =     f   x    2    X     j x < n   

g  a n d   

B     =     f   x    2    X     j x > n   

g  

7 3 1 8 9    

S t e p 3      S o r t p a r t i t i o n s a n d j o i n t h e m :  A    

+ +   n   

] + +    B    

w h e r e    A    

a n d   B    

a r e s o r t e d v e r s i o n s o f    A    

a n d   B     . 

1 3 7 8 9    

T h e J o y s o f S c h e m e : 1 9 ]  

Page 20: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 20/55

'  

&  

   $

   %

A n I l l u s t r a t i o n : Q u i c k s o r t  

R e p r e s e n t s e t s a s l i s t s    

( d e n e q u i c k s o r t    

( l a m b d a ( l s )    

( c o n d    

( ( n u l l ? l s ) ' ( ) )    

( e l s e ( l e t ( ( p i v o t ( c a r l s ) ) )    

( a p p e n d    

( q u i c k s o r t ( l o w e r - p a r t i t i o n p i v o t ( c d r l s ) ) )    

( l i s t p i v o t )    

( q u i c k s o r t ( u p p e r - p a r t i t i o n p i v o t ( c d r l s ) ) ) ) ) ) ) ) )    

  c o n d    i s a c o n d i t i o n a l .  

  l e t   i s a b i n d i n g c o n s t r u c t    

  a p p e n d     j o i n s t w o l i s t s .  

T h e J o y s o f S c h e m e : 2 0 ]  

Page 21: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 21/55

'  

&  

   $

   %

A n I l l u s t r a t i o n : Q u i c k s o r t  

( d e n e l o w e r - p a r t i t i o n    

( l a m b d a ( p i v o t l s )    

( l t e r - i n ( l a m b d a ( x ) (    >    

p i v o t x ) ) l s ) ) )    

( d e n e u p p e r - p a r t i t i o n    

( l a m b d a ( p i v o t l s )    

( l t e r - i n ( l a m b d a ( x ) (    <    

p i v o t x ) ) l s ) ) )    

( d e n e l t e r - i n    

( l a m b d a ( t e s t l s )    

( c o n d    

( ( n u l l ? l s ) ' ( ) )    

( ( t e s t ( c a r l s ) ) ( c o n s ( c a r l s ) ( l t e r - i n t e s t ( c d r l s ) ) ) )    

( e l s e ( l t e r - i n t e s t ( c d r l s ) ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 2 1 ]  

Page 22: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 22/55

'  

&  

   $

   %

U s i n g C o n t i n u a t i o n P a s s i n g S t y l e    

( d e n e q u i c k s o r t    

( l a m b d a ( l s )    

( c o n d    

( ( n u l l ? l s ) ' ( ) )    

( e l s e ( l e t ( ( p i v o t ( c a r l s ) ) )    

( p a r t i t i o n p i v o t ( c d r l s )    

( l a m b d a ( l o w e r - p a r t i t i o n u p p e r - p a r t i t i o n )    

( a p p e n d    

( q u i c k s o r t l o w e r - p a r t i t i o n )    

( l i s t p i v o t )    

( q u i c k s o r t u p p e r - p a r t i t i o n ) ) ) ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 2 2 ]  

Page 23: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 23/55

'  

&  

   $

   %

U s i n g C o n t i n u a t i o n P a s s i n g S t y l e  

( d e n e p a r t i t i o n    

( l a m b d a ( p i v o t l s k )    

( c o n d    

( ( n u l l ? l s ) ( k ' ( ) ' ( ) ) )    

( e l s e ( p a r t i t i o n p i v o t ( c d r l s )    

( l a m b d a ( l o w e r - p a r t i t i o n u p p e r - p a r t i t i o n )    

( i f (    <  ( c a r l s ) p i v o t )    

( k ( c o n s ( c a r l s ) l o w e r - p a r t i t i o n )    

u p p e r - p a r t i t i o n )    

( k l o w e r - p a r t i t i o n    

( c o n s ( c a r l s ) u p p e r - p a r t i t i o n ) ) ) ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 2 3 ]  

Page 24: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 24/55

'  

&  

   $

   %

S e m a n t i c s o f s o m e s p e c i a l f o r m s    

. . . I n t e r m s o f t h e b a s i c s y n t a x .  

( l e t ( ( v e ) . . . ) b o d y 0 b o d y 1 . . . )    )     

( ( l a m b d a ( v . . . ) b o d y 0 b o d y 1 . . . ) e . . . )    

( b l o c k ( v . . . ) b o d y 0 b o d y 1 . . . )    )     

( l e t ( ( v ' ) . . . ) b o d y 0 b o d y 1 . . . )    

( l e t r e c ( ( v e ) . . . ) b o d y 0 b o d y 1 . . . )    )     

( b l o c k ( v . . . ) ( s e t ! v e ) . . . b o d y 0 b o d y 1 . . . )    

( b e g i n e 0 e 1 . . . )    )     

( ( l a m b d a ( ) e 0 e 1 . . . ) )    

( w h i l e t e s t - e x p e 0 e 1 . . . )    )     

( l e t r e c    

( ( l o o p ( l a m b d a ( )    

( i f t e s t - e x p ( b e g i n e 0 e 1 . . . ( l o o p ) ) ) ) ) )    

( l o o p ) )    

T h e J o y s o f S c h e m e : 2 4 ]  

Page 25: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 25/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e    

F i x e d - p o i n t a l g o r i t h m s a r e f a i r l y c o m m o n      

  N u m e r i c a l A l g o r i t h m s .  

  S e t E q u a t i o n s .  

  P r o g r a m A n a l y s e s    e t c .  

R e p e a t a c e r t a i n c o m p u t a t i o n u n t i l c h a n g e s i n t h e s o -    

l u t i o n a r e w i t h i n a c e r t a i n b o u n d . F o r e x a m p l e , l e t u s    

c o m p u t e t h e v a l u e o f        . 

   = 4    t a n   

  1 

( 1 )  

t a n   

  1 

( 1 ) = 1         1   =  

3 + 1  =   5        1   =  

7 + 1  =   9  

: : : 

( d e n e p i  

( l e t r e c - e q u a t i o n s    

( ( x 0 ( + x (    *  ( e x p t - 1 n ) ( / 1 . 0 ( a d d 1 (    *  2 n ) ) ) ) )         )  

( n 0 ( a d d 1 n ) ( l a m b d a ( x y ) # t ) ) )    

(  *  4 x ) ) )    

T h e J o y s o f S c h e m e : 2 5 ]  

Page 26: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 26/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e  

I n s u c h c a s e s , o n e u s u a l l y b e g i n s w i t h a n i n i t i a l s o l u t i o n      

a n d t h e n i t e r a t e s .  

  l e t r e c - e q u a t i o n s    i s a s p e c i a l f o r m !  

  I t i s n o t i n s t a n d a r d S c h e m e .  

  I t i s a g r e a t a b s t r a c t i o n f o r a f a i r l y c o m m o n s t y l e o f    

p r o g r a m m i n g .  

  H o w g e n e r a l i s t h i s a b s t r a c t i o n ?    

T h e J o y s o f S c h e m e : 2 6 ]  

Page 27: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 27/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e  

S e t e q u a t i o n s a r e u s e f u l i n m a n y c o n t e x t s . F o r e x a m p l e :  

T h e P h i l o s o p h e r s P a r t y :  

  A p h i l o s o p h e r w i l l a t t e n d t h e p a r t y i h i s f r i e n d s w i l l .  

  F i n d a s u i t a b l e g u e s t - l i s t .  

( d e n e g u e s t - l i s t    

( l a m b d a ( i n i t s )    

( l e t r e c - e q u a t i o n s    

( ( g u e s t s i n i t s ( u n i o n g u e s t s ( f r i e n d s g u e s t s ) )        

) )  

g u e s t s ) ) )    

( g u e s t - l i s t ' ( a r i s t o t l e ) )    !     

T h e J o y s o f S c h e m e : 2 7 ]  

Page 28: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 28/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e  

  W e c a n a d d      l e t r e c - e q u a t i o n s    t o S c h e m e .  

  M a c r o s , o r s y n t a x e x t e n s i o n s , a l l o w s y n t a c t i c f o r m s    

t o b e a d d e d t o t h e l a n g u a g e .  

  M a c r o s a r e n o t y e t a p a r t o f s t a n d a r d S c h e m e , b u t    

w i l l s o o n b e .  

T h e J o y s o f S c h e m e : 2 8 ]  

Page 29: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 29/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e  

( l e t r e c - e q u a t i o n s ( ( v a r i n i t e x p b i n a r y - t e s t ) . . . )    

b o d y 0 b o d y 1 . . . )    

)     

( l e t r e c    

( ( l o o p ( l a m b d a ( v a r . . . )    

( i f ( a n d ( b i n a r y - t e s t v a r e x p ) . . . )    

( b e g i n b o d y 0 b o d y 1 . . . )    

( l o o p e x p . . . ) ) ) ) )    

( l o o p i n i t . . . ) )    

T h e J o y s o f S c h e m e : 2 9 ]  

Page 30: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 30/55

'  

&  

   $

   %

F i x e d - P o i n t s { A n o t h e r E x a m p l e  

( d e n e f r i e n d s    

( l e t ( ( l o o k u p - l i s t ' ( ( a r i s t o t l e p l a t o a l e x a n d e r )    

( p l a t o s o c r a t e s )    

( s o c r a t e s )    

( a l e x a n d e r p t o l e m y )    

( h o m e r v i r g i l s o p h o c l e s )    

( v i r g i l d a n t e h o r a c e )    

( d a n t e p e t r a c h )    

( p e t r a c h s i d n e y s h a k e s p e a r e ) ) ) )    

( l a m b d a ( g u e s t s )    

( m a p - u n i o n ( l a m b d a ( g )    

( l e t ( ( a n s ( a s s q g l o o k u p - l i s t ) ) )    

( i f a n s ( c d r a n s ) ' ( ) ) ) )    

g u e s t s ) ) ) )    

T h e J o y s o f S c h e m e : 3 0 ]  

Page 31: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 31/55

'  

&  

   $

   %

C o n t i n u a t i o n s    

  A g e n e r a l i z e d G o t o .  

  A c o n t i n u a t i o n i s a p r o c e d u r e t h a t r e p r e s e n t s t h e    

\ r e s t o f t h e c o m p u t a t i o n " .  

  A c o n t i n u a t i o n c a n b e c a p t u r e d .  

  I f a c o n t i n u a t i o n i s r e s t o r e d , i t r e s e t s t h e c o m p u t a t i o n      

b a c k t o t h e p o i n t t h e c o n t i n u a t i o n w a s c a p t u r e d .  

  P r i m a r y U s e s :  

{   N o n - s t a n d a r d c o n t r o l c o n s t r u c t s : c o r o u t i n e s .  

{   E x c e p t i o n H a n d l i n g      

{   B a c k t r a c k i n g , a s i n p r o l o g .  

T h e J o y s o f S c h e m e : 3 1 ]  

Page 32: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 32/55

'  

&  

   $

   %

E s c a p i n g P r o c e d u r e s    

P r o c e d u r e s t h a t a b o r t w h e n a p p l i e d . E x a m p l e s :  

( (     

* ( v ) ( + 3 v ) ) 4 )    !      7  

( + ( (       

* ( v ) ( + 3 v ) ) 4 ) 9 )    !      7  

( f ( (       

* ( v ) ( + 3 v ) ) 4 ) )    !     

7 ( F o r a n y f )    

( f ( (       

* ( v ) ( g ( + 3 v ) ) ) 4 ) )    !     

( g 7 )    

T h e J o y s o f S c h e m e : 3 2 ]  

Page 33: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 33/55

'  

&  

   $

   %

C a l l / c c | B a s i c s    

( c o n s 5 ( + 6 7 ) )    

( c a l l / c c    

( l a m b d a ( k )    

( k ( c o n s 5 ( + 6 7 ) ) ) ) )    

k = (     

* ( v ) v )    

( ( c a l l / c c    

( l a m b d a ( k ) ( k c o n s ) ) )    

5 ( + 6 7 ) )    

k = (     

* ( v ) ( v 5 ( + 6 7 ) ) )    

( c o n s    

( c a l l / c c    

( l a m b d a ( k ) ( k 5 ) ) )    

( + 6 7 ) )    

k = (     

* ( v ) ( c o n s v ( + 6 7 ) ) )    

T h e J o y s o f S c h e m e : 3 3 ]  

Page 34: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 34/55

'  

&  

   $

   %

C a l l / c c | B a s i c s  

( c o n s 5    

( c a l l / c c    

( l a m b d a ( k ) ( k ( + 6 7 ) ) ) ) )    

k = (     

* ( v ) ( c o n s 5 v ) )    

( c o n s 5    

( ( c a l l / c c    

( l a m b d a ( k ) ( k + ) ) )    

6 7 ) )    

k = (     

* ( v ) ( c o n s 5 ( v 6 7 ) ) )    

( c o n s 5    

( + ( c a l l / c c    

( l a m b d a ( k ) ( k 6 ) ) )    

7 ) )  

k = (     

* ( v ) ( c o n s 5 ( + v 7 ) ) )  

T h e J o y s o f S c h e m e : 3 4 ]  

Page 35: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 35/55

'  

&  

   $

   %

M i l e s t o n e s , D e v i l s a n d A n g e l s    

M i l e s t o n e :    A n i m p o r t a n t e v e n t | w o r t h g e t t i n g b a c k t o .  

D e v i l :    T a k e s y o u b a c k t o y o u r l a s t m i l e s t o n e i n e x c h a n g e    

f o r y o u r s o u l .  

A n g e l :    U n d o e s t h e w o r k o f t h e d e v i l .  

  T h e d e v i l ' s j o b i s t o k e e p t h e c o m p u t a t i o n f r o m h a p -    

p e n i n g : E r r o r s a n d e x c e p t i o n s .  

  A m i l e s t o n e i s a p l a c e w h e r e t h e c o m p u t a t i o n g o e s    

b a c k t o r e c o v e r .  

  A n a n g e l c a u s e s t h e c o m p u t a t i o n t o r e c o v e r .  

T h e J o y s o f S c h e m e : 3 5 ]  

Page 36: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 36/55

'  

&  

   $

   %

M i l e s t o n e s , D e v i l s a n d A n g e l s  

( d e n e m d a    

( l a m b d a ( )    

( c o n d    

( ( & ' a ( = ( & ' b ( m i l e s t o n e 3 ) ) 4 ) )    

( b e g i n ( w r i t e l n \ h e r e ! " ) ( & ' c ( a n g e l ' a l l - i s - w e l l ) ) ) )    

( ( & ' d ( = ( & ' e ( m i l e s t o n e 4 ) ) 5 ) )    

( & ' f ( a n g e l ( & ' g ( d e v i l 4 ) ) ) ) )    

( e l s e ( b e g i n ( w r i t e l n \ m a d e i t t o " ) ( & ' h ( d e v i l 5 ) ) ) ) ) ) )    

I n & b : 3    

I n & a : # f    

I n & e : 4      

I n & d : # f    

m a d e i t t o      

I n & e : 5    

I n & d : # t    

I n & b : 4      

I n & a : # t    

h e r e !  

I n & g : a l l - i s - w e l l  

I n & h : a l l - i s - w e l l  

a l l - i s - w e l l  

T h e J o y s o f S c h e m e : 3 6 ]  

Page 37: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 37/55

'  

&  

   $

   %

M i l e s t o n e s , D e v i l s a n d A n g e l s  

( d e n e    * 

p a s t    * 

' ( ) )    

( d e n e    *  f u t u r e    *  ' ( ) )    

( p u s h ! s v )    )     

( s e t ! s ( c o n s v s ) )    

( p o p ! s )    )     

( i f ( n u l l ? s )    

( e r r o r \ C a n n o t p o p e m p t y s t a c k " )    

( l e t ( ( v ( c a r s ) ) )    

( s e t ! s ( c d r s ) )    

v ) )  

T h e J o y s o f S c h e m e : 3 7 ]  

Page 38: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 38/55

'  

&  

   $

   %

M i l e s t o n e s , D e v i l s a n d A n g e l s  

( d e n e m i l e s t o n e    

( l a m b d a ( v )    

( c a l l / c c    

( l a m b d a ( k )    

( p u s h !   *  p a s t    *  k )  

v ) ) ) )    

( d e n e d e v i l  

( l a m b d a ( v )    

( c a l l / c c    

( l a m b d a ( k )    

( p u s h !   *  f u t u r e    *  k )  

( ( p o p !   *  p a s t    *  ) v ) ) ) ) )    

( d e n e a n g e l  

( l a m b d a ( v )    

( ( p o p !   *  f u t u r e    *  ) v ) ) )    

T h e J o y s o f S c h e m e : 3 8 ]  

Page 39: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 39/55

'  

&  

   $

   %

U s i n g C o n t i n u a t i o n s | E x c e p t i o n H a n d l i n g      

W e w a n t t o c o n t r o l h o w e r r o r s a r e h a n d l e d .  

( d e n e d i v - 3    

( l a m b d a ( x )    

( i f ( = x 0 ) ( r a i s e ' d i v i d e - b y - z e r o ) ( / 3 x ) ) ) )    

( d e n e f o o      

( l a m b d a ( n )    

( d i v - 3 n ) ) )    

( d e n e t e s t    

( w i t h - h a n d l e r ( ( d i v i d e - b y - z e r o ( l a m b d a ( e x n ) # f ) ) )    

( f o o 0 ) ) )    )     

# f  

  r a i s e     r a i s e s t h e e x c e p t i o n .  

  w i t h - h a n d l e r    t r a p s i t .  

T h e J o y s o f S c h e m e : 3 9 ]  

Page 40: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 40/55

'  

&  

   $

   %

U s i n g C o n t i n u a t i o n s | E x c e p t i o n H a n d l i n g  

H a n d l e r s h a v e      u i d s c o p e      i . e , t h e l a s t h a n d l e r e s t a b l i s h e d      

a t t h e t i m e t h e e r r o r o c c u r r e d m u s t b e c h o s e n .  

  S t a c k o f h a n d l e r s .  

  H a n d l e r s s h o u l d k n o w w h e r e t o r e s u m e e x e c u t i o n .  

( d e n e    *  h a n d l e r s    *  ' ( ) )    

( w i t h - h a n d l e r ( ( n a m e h a n d l e r ) ) b o d y 0 b o d y 1    : : : )  

)     

( c a l l / c c    

( l a m b d a ( k )    

( u i d - l e t ( (    *  h a n d l e r s    *  ( c o n s ( l i s t ' n a m e k h a n d l e r )    

*  h a n d l e r s    *  ) ) )  

b o d y 0 b o d y 1    : : :  ) ) )  

T h e J o y s o f S c h e m e : 4 0 ]  

Page 41: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 41/55

'  

&  

   $

   %

U s i n g C o n t i n u a t i o n s | E x c e p t i o n H a n d l i n g  

( d e n e r a i s e    

( l a m b d a ( e x n )    

( l e t ( ( r e s ( a s s q e x n    *  h a n d l e r s    *  ) ) )  

( c o n d    

( ( p a i r ? r e s )    

( a p p l y    

( l a m b d a ( n a m e k h a n d l e r )    

( k ( h a n d l e r e x n ) ) )    

r e s ) )    

( e l s e ( e r r o r \ E x c e p t i o n i s n o t h a n d l e d " e x n ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 4 1 ]  

Page 42: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 42/55

'  

&  

   $

   %

E n g i n e s | A m b ( S i m p l e V e r s i o n )    

E n g i n e s :    A n a b s t r a c t i o n o f a s y n c h r o n o u s c o n t r o l    

A m b :    R e t u r n t h e v a l u e o f t h e e x p r e s s i o n t h a t r e t u r n s    

r s t .  

>    ( a m b ( f a c t o r i a l 2 0 ) ( f a c t o r i a l 1 0 ) )    

( 6 . 2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0 )    

>    ( a m b ( f a c t o r i a l 2 0 ) ( f a c t o r i a l 1 0 ) )    

( 2 . 3 6 2 8 8 0 0 )    

>    ( a m b ( f a c t o r i a l 2 0 ) ( f a c t o r i a l 1 0 ) )    

( 2 . 3 6 2 8 8 0 0 )    

>    ( a m b ( f a c t o r i a l 2 0 ) ( f a c t o r i a l 1 0 ) )    

( 3 . 3 6 2 8 8 0 0 )    

>    ( a m b ( f a c t o r i a l 2 0 ) ( f a c t o r i a l 1 0 ) )    

( 1 1 . 2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0 )    

T h e J o y s o f S c h e m e : 4 2 ]  

Page 43: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 43/55

'  

&  

   $

   %

E n g i n e s | A m b ( S i m p l e V e r s i o n )  

  ( a n - e n g t i c k s s u c c e s s f a i l u r e )    

  T i c k s i s a n i n t e g e r .  

  S u c c e s s i s a 2 - a r g u m e n t p r o c e d u r e .  

  I t s r s t a r g u m e n t i s t h e u n u s e d t i c k s .  

  I t s s e c o n d a r g u m e n t i s t h e v a l u e o f t h e o r i g i n a l a r g u m e n t    

t o e n g i n e .  

  F a i l u r e i s a 1 - a r g u m e n t p r o c e d u r e .  

  I t s o n l y a r g u m e n t i s a n e w e n g i n e .  

  I f i t i s i n v o k e d , i t s t a r t s w h e r e t h e o l d o n e q u i t .  

T h e J o y s o f S c h e m e : 4 3 ]  

Page 44: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 44/55

'  

&  

   $

   %

E n g i n e s | A m b ( S i m p l e V e r s i o n )  

( a m b e 1 e 2 )    )     

( a m b / e n g i n e s ( e n g i n e e 1 ) ( e n g i n e e 2 ) )    

( d e n e a m b / e n g i n e s    

( l a m b d a ( e n g i n e 1 e n g i n e 2 )    

( e n g i n e 1 ( r a n d o m 2 0 ) c o n s    

( l a m b d a ( e n g i n e 1    * 

)  

( e n g i n e 2 ( r a n d o m 2 0 ) c o n s    

( l a m b d a ( e n g i n e 2    * 

)  

( a m b / e n g i n e s e n g i n e 1    *  e n g i n e 2    * 

) ) ) ) ) ) )    

W h a t i f c o n s i s r e p l a c e d b y ( l a m b d a ( t i c k s v a l u e ) v a l u e ) ?    

W h a t i f c o n s i s r e p l a c e d b y ( l a m b d a ( t i c k s v a l u e ) t i c k s ) ?    

T h e J o y s o f S c h e m e : 4 4 ]  

Page 45: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 45/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e    

  5 - p o i n t G a u s s i a n Q u a d r a t u r e a l g o r i t h m f o r n d i n g      

d e n i t e i n t e g r a l s .  

  T h e a l g o r i t h m i s e x a c t t o t h e 9 t h d e g r e e , m o d u l o t h e    

i n e x a c t n e s s o f t h e c o e c i e n t s u s e d .  

  S c h e m e ' s e l e g a n c e i s b r o u g h t o u t , a s u s u a l , b y      l a m b d a    

  T h e f o l l o w i n g a l g o r i t h m c o m p u t e s d e n i t e i n t e g r a l s    

o v e r t h e i n t e r v a l         1   :  0   ; 

+ 1  : 

0 ] , s o i n g e n e r a l , a t r a n s -    

l a t i o n o f t h e i n t e r v a l i s n e e d e d .  

  A n y o r d i n a r y l a n g u a g e l i k e C o r P a s c a l w o u l d r e q u i r e    

u s t o d e n e a \ h e l p " f u n c t i o n d e n e d o n t h e i n t e r v a l    

        1   ; + 1 ] , b u t w i t h S c h e m e ' s s u p p o r t o f a n o n y m o u s    

p r o c e d u r e s v i a      l a m b d a    , t h i s i s n ' t n e c e s s a r y .  

  T h e s u b s t i t u t i o n i s d o n e \ o n t h e y " .  

T h e J o y s o f S c h e m e : 4 5 ]  

Page 46: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 46/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

5 - p o i n t G a u s s i a n q u a d r a t u r e . E x a c t t o t h e    9  

t h 

d e g r e e .  

( d e n e i n t    

( l e t ( ( r o o t s ' ( 0 . 9 0 6 1 7 9 8 4 5 9 ; ; r o o t s o f    5  

t h 

L e g e n d r e p o l y    

0 . 5 3 8 4 6 9 3 1 0 1    

0 . 0 0 0 0 0 0 0 0 0 0    

- 0 . 5 3 8 4 6 9 3 1 0 1    

- 0 . 9 0 6 1 7 9 8 4 5 9 ) )    

( c o e s ' ( 0 . 2 3 6 9 2 6 8 8 5 0 ; ; c o e s f o r i t s r e s p e c t i v e r o o t    

0 . 4 7 8 6 2 8 6 7 0 5    

0 . 5 6 8 8 8 8 8 8 8 9    

0 . 4 7 8 6 2 8 6 7 0 5    

0 . 2 3 6 9 2 6 8 8 5 0 ) ) )    

T h e J o y s o f S c h e m e : 4 6 ]  

Page 47: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 47/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

( l e t ( ( i n t - f r o m - n e g - 1 - t o - 1    

( l a m b d a ( f )    

( l e t r e c    

( ( l o o p      

( l a m b d a ( c o e s r o o t s )    

( c o n d    

( ( n u l l ? c o e s ) 0 . 0 )    

( e l s e    

( + (   *  ( c a r c o e s ) ( f ( c a r r o o t s ) ) )    

( l o o p ( c d r c o e s ) ( c d r r o o t s ) ) ) ) ) ) ) )    

( l o o p c o e s r o o t s ) ) ) ) )    

( l a m b d a ( f l i m i t - p a i r )    

( i n t - f r o m - n e g - 1 - t o - 1    

( t r a n s l a t e f l i m i t - p a i r ( c o n s - 1 1 ) ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 4 7 ]  

Page 48: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 48/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

( d e n e t r a n s l a t e    

( l a m b d a ( f l i m i t - p a i r 1 l i m i t - p a i r 2 )    

( l e t ( ( f r o m 1 ( c a r l i m i t - p a i r 1 ) )    

( t o 1 ( c d r l i m i t - p a i r 1 ) )    

( f r o m 2 ( c a r l i m i t - p a i r 2 ) )    

( t o 2 ( c d r l i m i t - p a i r 2 ) ) )    

( l e t ( ( m ( / ( - t o 1 f r o m 1 ) ( - t o 2 f r o m 2 ) ) ) )    

( l a m b d a ( x )    

(  *  m ( f ( + (    *  m ( - x f r o m 2 ) ) f r o m 1 ) ) ) ) ) ) ) )    

T h e J o y s o f S c h e m e : 4 8 ]  

Page 49: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 49/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

x   

d x    = 1  =   3  

>    ( i n t ( l a m b d a ( x ) (    *  x x ) ) ( c o n s 0 . 0 1 . 0 ) )    

0 . 3 3 3 3 3 3 3 3 3 3 0 4 4 6 1 3    

T o c o m p u t e a d o u b l e i n t e g r a l w e n e e d t o m a k e s u r e w e    

g e t t h e v a r i a b l e s a s s i g n e d c o r r e c t l y :  

x y :  d y   : 

d x    = 2 1  =   4   

>    ( i n t    

( l a m b d a ( x )    

( i n t    

( l a m b d a ( y ) (    *  x y ) )    

( c o n s 3 . 0 4 . 0 ) ) )    

( c o n s 1 . 0 2 . 0 ) )    

5 . 2 4 9 9 9 9 9 9 9 4 7 5    

T h e J o y s o f S c h e m e : 4 9 ]  

Page 50: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 50/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

B u t w i t h S c h e m e ' s a b i l i t y t o c u r r y         

' s w e c a n s u r e l y d o      

b e t t e r :  

>    ( d e n e f ( l a m b d a ( x ) ( l a m b d a ( y ) (    *  x y ) ) ) )    

>    ( i n t ( l a m b d a ( x )    

( i n t ( l a m b d a ( y ) ( ( f x ) y ) )    

( c o n s 3 . 0 4 . 0 ) ) )    

( c o n s 1 . 0 2 . 0 ) )    

5 . 2 4 9 9 9 9 9 9 9 4 7 5    

W e c a n n o w d e n e d o u b l e - i n t a s f o l l o w s :  

( d e n e d o u b l e - i n t    

( l a m b d a ( f l i m i t - p a i r 1 l i m i t - p a i r 2 )    

( i n t ( l a m b d a ( x )    

( i n t ( l a m b d a ( y ) ( ( f x ) y ) )    

l i m i t - p a i r 2 ) )    

l i m i t - p a i r 1 ) ) )    

T h e J o y s o f S c h e m e : 5 0 ]  

Page 51: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 51/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

>    ( d o u b l e - i n t    

( l a m b d a ( x ) ( l a m b d a ( y ) (    *  x y ) ) )    

' ( 1 . 0 . 2 . 0 ) ' ( 3 . 0 . 4 . 0 ) )    

5 . 2 4 9 9 9 9 9 9 9 4 7 5    

W e c a n w r i t e a n i n t e g r a t o r t h a t t a k e s m u l t i p l e i n t e g r a t i o n s .  

( d e n e n - i n t    

( l a m b d a ( f l i m i t - p a i r s )    

( c o n d    

( ( n u l l ? l i m i t - p a i r s ) f )    

( e l s e ( i n t ( l a m b d a ( x )    

( n - i n t ( f x ) ( c d r l i m i t - p a i r s ) ) )    

( c a r l i m i t - p a i r s ) ) ) ) ) )    

>    ( n - i n t    

( l a m b d a ( x ) ( l a m b d a ( y ) (    *  x y ) ) )    

' ( ( 1 . 0 . 2 . 0 ) ( 3 . 0 . 4 . 0 ) ) )    

T h e J o y s o f S c h e m e : 5 1 ]  

Page 52: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 52/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

W e c a n a b s t r a c t o v e r i n t , a l l o w i n g a n y i n t e g r a t o r .  

( d e n e n - i n t - m a k e r    

( l a m b d a ( i n t )    

( l e t r e c    

( ( n - i n t    

( l a m b d a ( f l i m i t - p a i r s )    

( c o n d    

( ( n u l l ? l i m i t - p a i r s ) f )    

( e l s e ( i n t ( l a m b d a ( x )    

( n - i n t ( f x ) ( c d r l i m i t - p a i r s ) ) )    

( c a r l i m i t - p a i r s ) ) ) ) ) ) )    

n - i n t ) ) )    

( d e n e g a u s s i a n - q u a d r a t u r e - n - i n t ( n - i n t - m a k e r i n t ) )    

T h e J o y s o f S c h e m e : 5 2 ]  

Page 53: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 53/55

'  

&  

   $

   %

G a u s s i a n Q u a d r a t u r e  

O r w e c a n a s s o c i a t e a d i e r e n t i n t e g r a t i o n m e t h o d    

w i t h e a c h l i m i t p a i r b y f o r m i n g a n e w d a t a s t r u c t u r e    

t h a t h a s b o t h t h e i n t e g r a t o r a n d i t s l i m i t p a i r .  

( d e n e s u p e r - n - i n t    

( l a m b d a ( f i n t - l i m i t s - p a i r s )    

( c o n d    

( ( n u l l ? i n t - l i m i t s - p a i r s ) f )    

( e l s e ( ( c a r ( c a r i n t - l i m i t s - p a i r s ) )    

( l a m b d a ( x )    

( s u p e r - n - i n t ( f x ) ( c d r i n t - l i m i t s - p a i r s ) ) )    

( c d r ( c a r i n t - l i m i t s - p a i r s ) ) ) ) ) ) )    

>    ( s u p e r - n - i n t    

( l a m b d a ( x ) ( l a m b d a ( y ) (    *  x y ) ) )    

( l i s t ( c o n s i n t ' ( 1 . 0 . 2 . 0 ) ) ( c o n s i n t ' ( 3 . 0 . 4 . 0 ) ) ) )    

T h e J o y s o f S c h e m e : 5 3 ]  

Page 54: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 54/55

'  

&  

   $

   %

T h e S i m p l i c i t y o f S c h e m e    

  v a r i a b l e r e f e r e n c e , l a m b d a , a p p l i c a t i o n    

  s e t !  

  i f  

  c a l l / c c    

  e n g i n e s    

  P r i m i t i v e s o n d a t a s t r u c t u r e s    

  d e n e    

)     

T h e J o y s o f S c h e m e : 5 4 ]  

Page 55: The Joys of Scheme

8/14/2019 The Joys of Scheme

http://slidepdf.com/reader/full/the-joys-of-scheme 55/55

'  

&  

   $

   %

W h y S c h e m e ?    

  P a r a d i g m a t i c C o n s i d e r a t i o n s    

{   S i m p l i c i t y      

{   M i n i m a l i t y      

{   F l e x i b i l i t y      

{   W i d e r a n g e o f c o n c e p t s c a n b e r e a l i z e d w i t h j u s t    

a f e w c o n s t r u c t s .  

  T e c h n i c a l C o n s i d e r a t i o n s    

{   I n t e r a c t i v e P r o g r a m m i n g .  

{   A u t o m a t i c M e m o r y M a n a g e m e n t a n d T y p e S a f e t y      

{   I m p l e m e n t a t i o n s o n a w i d e v a r i e t y o f p l a t f o r m s    

  W h y n o t ?