Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Common Lisp坑爹简介 Liutos
[email protected]
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Outline 1
Tool Chain
2
Program Structure
3
Data Types
4
Functions
5
Special Operators
6
Multiple Values Return
7
CLOS
8
Macro Facility
9
Readtable
10
Things Left Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Development Tools Editor: GNU Emacs Emacs Plugins: SLIME, paredit-mode Common Lisp Implementations: SBCL, CCL, CLISP, ... Package Manager: Quicklisp Building System: ASDF
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Form Categories Symbols as Forms Example boundp let* multiple-value-bind ++
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Form Categories Symbols as Forms Example boundp let* multiple-value-bind ++ Conses as Forms Example (setq x 1) (boundp ’x)
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Form Categories Symbols as Forms Example boundp let* multiple-value-bind ++ Conses as Forms Example (setq x 1) (boundp ’x) Self-Evaluating Objects Example 3 #c(2/3 5/8) "Hello, world."
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Date Types Number Character Symbol List and Cons Array Vector String Bit-Vector
Liutos
[email protected]
Hash-Table Readtable Package Pathname Stream Random-State Structure Function
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Define Functions(Part 1) - Lambda and Defun Lambda is used for defining a new anonymous function Example (lambda (x) (* x 2)) Defun is used for defining or redefining a named function Example ( defun t i m e s 2 ( n ) ( ∗ n 2 ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Define Functions(Part 2) - Lambda List Definition Lambda list is a list for describing how the arguments are received when calling a function lambda list keywords &allow-other-keys &aux
&key &optional
&rest
Example ( d e f u n f n 1 ( a &o p t i o n a l b ) ( l i s t a b ) ) ( f n 1 1 ) => ( 1 NIL ) ( f n 1 1 2 ) => ( 1 2 ) ( d e f u n f n 2 ( a &k e y b c ) ( l i s t a b c ) ) ( f n 2 1 ) => ( 1 NIL NIL ) ( f n 2 1 : c 2 ) => ( 1 NIL NIL ) ( d e f u n f n 3 ( a &r e s t a r g s ) ( l i s t a a r g s ) ) ( f n 3 1 2 3 4 5 ) => ( 1 ( 2 3 4 5 ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Special Operators Definition The operator treats its subexpressions with special syntax and evaluating rules block catch eval-when flet function go if labels let
let* load-time-value locally macrolet multiple-value-call multiple-value-prog1 progn progv quote
Liutos
[email protected]
return-from setq symbol-macrolet tagbody the throw unwind-protect
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Examples for Special Operators 1
GOTO in C(from binghe) int fact ( int n) { int r e s u l t = 1; a: r e s u l t ∗= n−−; i f ( n > 0 ) goto a ; return r e s u l t ; }
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Examples for Special Operators 1
GOTO in C(from binghe) int fact ( int n) { int r e s u l t = 1; a: r e s u l t ∗= n−−; i f ( n > 0 ) goto a ; return r e s u l t ; }
2
GOTO in Common Lisp(from binghe, too) ( defun f a c t ( n ) ( l e t (( r e s u l t 1)) ( tagbody a ( s e t q r e s u l t (∗ r e s u l t n ) ) ( decf n) ( i f (> n 0 ) ( go a ) ) ) r e s u l t ))
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s multiple values return Definition One function call yields any number of values Example 1
(values 1 2 3) => 1, 2, 3
2
(truncate 10 3) => 3, 1
3
(ceiling pi) => 4, -0.8584073464102069d0
4
(intern "ABC") => ABC, NIL
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Advantages(Part 1) - Avoid Construction Spliting a string from given position Example 1
Version 1: Using construction. ( defun s p l i t − s t r i n g ( s t r i n g p o s i t i o n ) ( cons ( subseq s t r i n g 0 p o s i t i o n ) ( subseq s t r i n g p o s i t i o n ) ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Advantages(Part 1) - Avoid Construction Spliting a string from given position Example 1
Version 1: Using construction. ( defun s p l i t − s t r i n g ( s t r i n g p o s i t i o n ) ( cons ( subseq s t r i n g 0 p o s i t i o n ) ( subseq s t r i n g p o s i t i o n ) ) )
2
Version 2: Using multiple values return. ( defun s p l i t − s t r i n g ( s t r i n g p o s i t i o n ) ( v a l u e s ( subseq s t r i n g 0 p o s i t i o n ) ( subseq s t r i n g p o s i t i o n ) ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Advantages(Part 2) - Avoid Different Meanings Of Return Value Example small ( d e f v a r ∗ h t ∗ ( make−hash−table ) ) ( s e t f ( gethash 1 ∗ h t ∗ ) ’ n i l ) ( gethash 1 ∗ h t ∗ ) => NIL , T ( gethash 2 ∗ h t ∗ ) => NIL , NIL
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Common Lisp Object System Definition The facility for implementing the object-oriented programming paradigm in Common Lisp Functionalities and Features 1 Class-based 2
Define new classes
3
Define new generic functions and method
4
Multiple inheritance
5
Metaobject Protocol
6
Method combinator and define new method combinators
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Examples 1
Define a new class ( defclass person () ( name age ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Examples 1
Define a new class ( defclass person () ( name age ) )
2
Define the same class with more slot options ( defclass person () ( ( name : i n i t a r g : name : a c c e s s o r person−name : i n i t f o r m ” d e f a u l t name” ) ( age : i n i t a r g : age : a c c e s s o r person−age : type i n t e g e r )))
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s MACRO Definition Macro is a facility for transforming one expression to another expression and then evaluates it.
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s MACRO Definition Macro is a facility for transforming one expression to another expression and then evaluates it. Usages of Macro 1
Transformation
2
Establish bindings
3
Controls the evaluation of each arguments
4
Access the context
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Define New Control Structures DO = TAGBODY + GO Example ( defmacro my−do ( v a r l i s t e n d l i s t &body body ) ( l e t ( ( e x i t − p o i n t ( gensym ) ) ( body−point ( gensym ) ) ) ‘( block n i l ( l e t , ( mapcar # ’( lambda ( v l ) ( l i s t ( f i r s t v l ) ( s e c o n d v l ) ) ) v a r l i s t ) ( tagbody ( go , e x i t − p o i n t ) , body−point ( p r o g n , @body ) ( p s e t q ,@( mapcar # ’( lambda ( v l ) ( l i s t ( f i r s t v l ) ( t h i r d v l ) ) ) v a r l i s t )) , exit−point ( u n l e s s , ( c a r e n d l i s t ) ( go , body−point ) ) ( r e t u r n − f r o m n i l ( p r o g n ,@( c d r e n d l i s t ) ) ) ) ) ) ) )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
SETF: General Assignment Regular assignment
Access slots
Example
Example
( d e f v a r ∗ h t ∗ ( make−hash−table ) ) ( s e t f ∗ht∗ n i l ) ∗ h t ∗ => NIL
( d e f c l a s s foo () (( a : i n i t a r g : a ))) ( d e f v a r ∗ i ∗ ( make−instance ’ f o o : a 1 2 3 ) ) ( s l o t − v a l u e ∗ i ∗ ’ a ) => 123 ( s e t f ( s l o t − v a l u e ∗ i ∗ ’ a ) 321) ( s l o t − v a l u e ∗ i ∗ ’ a ) => 321
Access places Example ( d e f v a r ∗a∗ ( c o n s 1 2 ) ) ∗a∗ => ( 1 . 2 ) ( s e t f ( c d r ∗a ∗) 3 ) ∗a∗ => ( 1 . 3 )
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s readtable The object contains associations between macro characters and their reader macro functions and controls the behavior of the reader.
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s readtable The object contains associations between macro characters and their reader macro functions and controls the behavior of the reader. Pre-Defined Macro Characters ( ) ’ ; " ‘ , #
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
What’s readtable The object contains associations between macro characters and their reader macro functions and controls the behavior of the reader. Pre-Defined Macro Characters ( ) ’ ; " ‘ , # Customizes the readtable for adding new lexical rules Adds a syntactic sugar in Common Lisp for reading hash tables
Liutos
[email protected]
Common Lisp坑爹简介
Tool Chain Program Structure Data Types Functions Special Operators Multiple Values Return CLOS Macro Facility Readta
Something Else Intresting 1
Loop Macro - DSL for iteration
2
Pretty Printing
3
MOP - Metaobject Protocol
4
Condition System - Signal, Handle and Restart.
Liutos
[email protected]
Common Lisp坑爹简介