Slide 1

Slide 1 text

A Brief Introduction to Common Lisp David Gu [email protected]

Slide 2

Slide 2 text

A Brief History Originally specified in 1958, Lisp is the second-oldest high- level programming language in widespread use today; only Fortran is older (by one year). Lisp stands for LISt Processing (while Fortran stands for FORmula TRANslator). 1958 ~ 1980: Various dialects and systems, MacLisp, InterLisp, and Lisp Machines.

Slide 3

Slide 3 text

A Brief History 1975 ~ 1980: Scheme, the first dialect choosing lexical scope, developed in MIT. 1980 ~ 1990: Common Lisp, an industry-level language, published in ANSI standard. 1990 ~ Now: Various implementation for Common Lisp and Scheme; More 3rd party libraries; A new successful dialect Clojure.

Slide 4

Slide 4 text

First Impression / / C C o d e i n t f a c t o r i a l ( i n t n ) { i f ( n = = 0 ) r e t u r n 1 ; e l s e r e t u r n n * f a c t o r i a l ( n - 1 ) ; } ; ; C o m m o n L i s p C o d e ( d e f u n f a c t o r i a l ( n ) ( i f ( = n 0 ) 1 ( * n ( f a c t o r i a l ( - n 1 ) ) ) ) )

Slide 5

Slide 5 text

First Impression: Evolution / / C c o d e , u s i n g t a i l r e c u r s i o n i n t f a c t o r i a l _ h e l p e r ( i n t r e s u l t , i n t c o u n t ) { i f ( c o u n t = = 0 ) r e t u r n r e s u l t ; e l s e r e t u r n f a c t o r i a l _ h e l p e r ( r e s u l t * c o u n t , c o u n t - 1 ) ; } i n t f a c t o r i a l ( i n t n ) { r e t u r n f a c t o r i a l _ h e l p e r ( 1 , n ) ; }

Slide 6

Slide 6 text

First Impression: Evolution ; ; C o m m o n L i s p c o d e , u s i n g t a i l r e c u r s i o n ( d e f u n f a c t o r i a l ( n ) ( d e c l a r e ( o p t i m i z e ( s p e e d 3 ) ) ) ( l a b e l s ( ( i t e r ( r e s u l t c o u n t ) ( i f ( = c o u n t 0 ) r e s u l t ( i t e r ( * r e s u l t c o u n t ) ( 1 - c o u n t ) ) ) ) ) ( i t e r 1 n ) ) )

Slide 7

Slide 7 text

First Impression: Final 'Product' ; ; ; ; c a n e v e n u s e a f u n c t i o n c a l l e d ‘ d i s a s s e m b l e ’ ; ; ; ; t o c h e c k t h e a s s e m b l e c o d e . ( d e f u n f a c t o r i a l ( n ) ( d e c l a r e ( o p t i m i z e ( s p e e d 3 ) ) ) ( l a b e l s ( ( i t e r ( r e s u l t c o u n t ) ( d e c l a r e ( t y p e f i x n u m r e s u l t c o u n t ) ) ( i f ( = c o u n t 0 ) r e s u l t ( i t e r ( t h e f i x n u m ( * r e s u l t c o u n t ) ) ( t h e f i x n u m ( - c o u n t 1 ) ) ) ) ) ) ( i t e r 1 n ) ) )

Slide 8

Slide 8 text

Multi-Paradigms: Functional Lambda(λ) ( ( l a m b d a ( x y ) ( + x y ) ) 1 2 ) = > 3 Map ( m a p ' l i s t # ' ( l a m b d a ( x ) ( 1 + x ) ) ( l i s t 0 1 2 3 ) ) = > ( 1 2 3 4 )

Slide 9

Slide 9 text

Multi-Paradigms: Functional Filter = > ( 1 3 5 ) Fold(foldr in ML) ( r e d u c e # ' + ( l i s t 1 2 3 4 5 ) ) = > 1 5 Curried Function, Lazy Evaluation, and more...

Slide 10

Slide 10 text

Multi-Paradigms: Imperative Common Lisp does provide imperative operators like: ( s e t f x 1 0 ) ⇔ x : = 1 0 And for functional functions, Common Lisp also provides their ‘destructive’ version: map ⇔ map-into filter (remove-if-not) ⇔ delete-if-not And even more: goto, for, while...

Slide 11

Slide 11 text

Multi-Paradigms: OOP Common Lisp has its own implementation for object oriented programming, which is called CLOS. Unlike Java or C++, Common Lisp uses an approach called Generic Function instead of Message Passing. Basically, all the methods belongs to generic function instead of a specific class.

Slide 12

Slide 12 text

Multi-Paradigms: OOP For example, if there's a human class and there's a method called s p e a k , then I create an instance of human called 'me': In message passing style: m e . s p e a k ( " H e l l o " ) In generic function style: s p e a k ( m e , " H e l l o " )

Slide 13

Slide 13 text

Multi-Paradigms: OOP ; ; ; C L O S E x a m p l e ( d e f c l a s s h u m a n ( ) ( ( n a m e : i n i t f o r m " A n o n y m o u s " : i n i t a r g : n a m e : a c c e s s o r n a m e ) ) ) ( d e f c l a s s m a n ( h u m a n ) ( ) ) ( d e f c l a s s w o m a n ( h u m a n ) ( ) ) ( d e f g e n e r i c s a y - l o v e ( h u m a n ) ( : d o c u m e n t a t i o n " A h u m a n s a y s : ' I l o v e y o u ! ' " ) ) ( d e f m e t h o d s a y - l o v e ( ( o b j h u m a n ) ) ( f o r m a t t " ~ a s a y s : ' I l o v e y o u ! ' ~ % " ( n a m e o b j ) ) ) ( d e f m e t h o d s a y - l o v e : a f t e r ( ( o b j m a n ) ) ( f o r m a t t " I n ~ a ' s m i n d : H m m . . . W h y s h o u l d I s a y t h a t ? ~ % " ( d e f v a r r e m e o ( m a k e - i n s t a n c e ' m a n : n a m e " R o m e o " ) ) ( d e f v a r j u l i e t ( m a k e - i n s t a n c e ' w o m a n : n a m e " J u l i e t " ) )

Slide 14

Slide 14 text

Multi-Paradigms: OOP A 'men' will have different behavior. C L - U S E R > ( s a y - l o v e j u l i e t ) J u l i e t s a y s : ' I l o v e y o u ! ' N I L C L - U S E R > ( s a y - l o v e r e m e o ) R o m e o s a y s : ' I l o v e y o u ! ' I n R o m e o ' s m i n d : H m m . . . W h y s h o u l d I s a y t h a t ? N I L It’s called method combination.

Slide 15

Slide 15 text

What Makes Lisp Special? 1. S-Expression 2. Macro

Slide 16

Slide 16 text

S-Expression In Common Lisp, a s-exp would be like: s-exp : (op s-exp1 s-exp2 ...) op: a function | a macro | a special form And interestingly, a s-exp could be ‘made’ like this: ( c o n s 1 2 ) = > ( 1 . 2 ) ( c o n s 1 ( c o n s 2 n i l ) ) = > ( 1 . ( 2 . n i l ) ) = > ( 1 2 ) ( c o n s ' + ( c o n s 1 ( c o n s 2 n i l ) ) ) = > ( + 1 2 ) ( e v a l ( c o n s ' + ( c o n s 1 ( c o n s 2 n i l ) ) ) ) = > 3

Slide 17

Slide 17 text

S-Expression A s-exp looks like a linked list but actually a tree. ( c a r ( l i s t ' + 1 2 ) ) = > ' + ( c d r ( l i s t ' + 1 2 ) ) = > ( 1 2 ) Writing s-expressions is actually writing the Abstract Syntax Tree(AST).

Slide 18

Slide 18 text

S-Expression: Benefits There will be no lexical analysis because all the codes already are the AST. There will be no need to worry about the Operator Precedence. Very convenient to represent data structures like trees and graphs.

Slide 19

Slide 19 text

Macro: Why Lisp is Called Programmable Programming Language? Unlike functions, macros will be expanded first before evaluated; After expanding finished, the whole s-expression generated by macro will be evaluated; So a macro is actually a function that transforms arguments to s-expressions.

Slide 20

Slide 20 text

Macro: A Simple Example In this case, it acts like a inline function. C L - U S E R > ( d e f u n a d d ( x y ) ( + x y ) ) A D D C L - U S E R > ( d e f m a c r o a d d - 1 ( x y ) ` ( + , x , y ) ) A D D - 1 C L - U S E R > ( a d d 1 0 2 0 ) 3 0 C L - U S E R > ( a d d - 1 1 0 2 0 ) 3 0 C L - U S E R > ( m a c r o e x p a n d ' ( a d d - 1 1 0 " s t r i n g " ) ) ( + 1 0 " s t r i n g " ) T

Slide 21

Slide 21 text

Macro: A Real (but a little silly) Example Macros can do something that a function can never do. ( d e f m a c r o d e l a y ( t h i n g ) ; ; r e t u r n a t h u n k ` ( l a m b d a ( ) , t h i n g ) ) ( d e f u n f o r c e ( t h u n k ) ( f u n c a l l t h u n k ) ) ( d e f m a c r o m y - i f ( t e s t t h e n e l s e ) ` ( b l o c k n i l ( a n d , t e s t ( r e t u r n , t h e n ) ) , e l s e ) ) ( d e f u n f a c t o r i a l ( n ) ( m y - i f ( = n 0 ) ; ; t e s t 1 ; ; t h e n ( * n ( f a c t o r i a l ( - n 1 ) ) ) ) ) ; ; e l s e

Slide 22

Slide 22 text

Macro: A Real Example Macros can do something that a function can never do. C L - U S E R > ( d e l a y ( l o o p ) ) # < F U N C T I O N ( L A M B D A ( ) ) { 1 0 0 3 A F 4 E B B } > C L - U S E R > ( m a c r o e x p a n d ' ( d e l a y ( l o o p ) ) ) # ' ( L A M B D A ( ) ( L O O P ) ) T C L - U S E R > ( f o r c e ( d e l a y ( + 1 2 ) ) ) 3 C L - U S E R > ( f a c t o r i a l 2 0 ) 2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0 C L - U S E R > ( m a c r o e x p a n d ' ( m y - i f ( = n 0 ) ; t e s t 1 ; t h e n ( * n ( f a c t o r i a l ( - n 1 ) ) ) ) ) ; e l s e ( B L O C K N I L ( A N D ( = N 0 ) ( R E T U R N 1 ) ) ( * N ( F A C T O R I A L ( - N 1 ) ) ) ) T

Slide 23

Slide 23 text

Macro: Define New Syntax ; ; ; ; d e f i n e n e w s y n t a x b y m a c r o ( d e f m a c r o w h i l e ( t e s t & b o d y b o d y ) ` ( d o ( ) ( ( n o t , t e s t ) ) , @ b o d y ) ) ( d e f m a c r o f o r ( v a r s t a r t e n d & b o d y b o d y ) ( l e t ( ( g e n d ( g e n s y m ) ) ) ` ( d o ( ( , v a r , s t a r t ( 1 + , v a r ) ) ( , g e n d , e n d ) ) ( ( > , v a r , g e n d ) ) , @ b o d y ) ) ) C L - U S E R > ( l e t ( ( x 5 ) ) ( w h i l e ( > x 0 ) ( f o r m a t t " ~ d " x ) ( d e c f x ) ) ) 5 4 3 2 1 N I L C L - U S E R > ( f o r i 1 5 ( f o r m a t t " ~ d " i ) ) 1 2 3 4 5 N I L

Slide 24

Slide 24 text

Macro: Even Let Evaluation Happens During 'Compiling' Time As mentioned before, a macro will be expanded before evaluated, even before compiled. C L - U S E R > ( d e f m a c r o a v g ( & r e s t n u m b e r s ) ` ( / ( + , @ n u m b e r s ) , ( l e n g t h n u m b e r s ) ) ) A V G C L - U S E R > ( a v g 1 2 3 4 5 6 7 8 9 1 0 ) 1 1 / 2 C L - U S E R > ( m a c r o e x p a n d ' ( a v g 1 2 3 4 5 6 7 8 9 1 0 ) ) ( / ( + 1 2 3 4 5 6 7 8 9 1 0 ) 1 0 ) T 'Counting how many numbers' happens before run time.

Slide 25

Slide 25 text

Lisp: An ‘Edge’ of Programming Languages

Slide 26

Slide 26 text

Thanks for Watching! We toast the Lisp programmer who pens his thoughts within nests of parentheses. -- Alan J. Perlis 's foreword