Você está na página 1de 41

LISP

Ilya Korsunsky

Outline
History etc. Core LISP Extending LISP Conclusions: good, bad and ugly

John Foderaro: Lisp is a programmable programming language


http://wwwformal.stanford.edu/jmc/history/lisp/lisp.html

Outline
History etc. Core LISP
Code Processing Variables Functions Macros Special Operators The Rest

Extending LISP Conclusions: good, bad and ugly

Lists
Linked lists of con cells Heterogeneous Hierarchical Some operations on lists include
..

Coding Processing Done in Two Steps


Reader: character string -> S-expressions Evaluator: S-expressions -> LISP forms
Compiling: LISP forms turned into machine code Evaluating: LISP forms evaluated to return value

S-expressions
Consist of lists and atoms List: white-space separated elements enclosed by parentheses
Each element is an S-expression List = ( S-exp1 S-exp2 S-expN )

Atom: everything else


Numbers Strings Names

S-expressions
Numbers
Rational (Integers and ratios), floating point and complex

Strings
Enclosed by double quotes Backslash (/) escapes next character

Names
Represented by symbol objects Can contain almost any character (with a few rules) Reader converts all unescaped characters into UPPER CASE (foo -> FOO but \foo = Foo) Package: a symbol table

S-expressions Are Translated into LISP Forms


Not all S-expressions are valid LISP forms 2 Legal forms:
Atom List whose first element is a symbol

How are LISP forms evaluated?

LISP Forms: Atoms


Atom: Symbols and Everything Else Symbol:
Name of a variable Evaluated to current value of variable

Self-evaluating objects:
Numbers, strings, etc.

Exception: self-evaluating symbols


t, Nil, keyword symbols (:x)

LISP Forms: Lists


List Forms are Defined by the Opening Symbol
Function Calls Special Operators Macros

Outline
History etc. Core LISP
Code Processing Functions Variables Macros The Rest

Extending LISP Conclusions: good, bad and ugly

Defining a Function

Name: symbol Parameters: list Documentation: string Body-forms: lists

Passing Parameters to a Function


Required:
(defun foo (a b c) (list a b c))

Optional: Default value = Nil unless specified


(defun foo1 (a b &optional c d) (list a b c)) (foo1 1 2) -> (1 2 Nil Nil) (defun foo2 (a b &optional (c 10) d) (list a b c)) (foo2 1 2) -> (1 2 10 Nil)

Passing Parameters to a Function


Rest Parameters: Expect the unexpected
Can take any number of parameters
(defun foo (&rest a) a) (foo 1 2 3) -> (1 2 3)

Used in format and +


(defun format (stream string &rest values) ) (defun + (&rest numbers) )

Passing Parameters to a Function


Keyword Parameters: Provide arbitrary subset of optional parameters.
With (&optional a b c d), to provide value for d, I need to provide values for a, b and c (defun foo (&key (a 0) b (c 10)) (list a b c))
(foo) -> (0 Nil 10) (foo :a 50) -> (50 Nil 10) (foo :b 70 :c 2) -> (0 70 2)

Return Values
Method ONE: last line of function is the return value. Method TWO: use return-from (special operator) to return from anywhere in the function (return-from name return-val)

Functions as Data
Accessing a function object:
(function foo) #foo

Running a function object:


(funcall #foo 1 2 3)
Know the number of parameters to foo

(apply #foo data-list)


Expects a single list

Anonymous Functions
Create a lambda function (lambda (parameters) body) Call a lambda function (funcall (lambda (x y) (+ x y)) (2 3) Used for creating closures

Functions Evaluate Parameters Before Running Own Code


For control over how parameters are evaluated, use
Macros Special Operators To come immediately after Variables

Outline
History etc. Core LISP
Code Processing Functions Variables Macros The Rest

Extending LISP Conclusions: good, bad and ugly

Variables
Variable is a reference to any kind of object Dynamically typed (types checked at runtime) Strongly typed (object cannot be cast into another class)

Lexical Variables

Dynamic Variables

Constant Variables

Variable Assignment

Outline
History etc. Core LISP
Code Processing Functions Variables Macros The Rest

Extending LISP Conclusions: good, bad and ugly

Predefined Macros

Outline
History etc. Core LISP
Code Processing Functions Variables Macros The Rest

Extending LISP Conclusions: good, bad and ugly

Everything Else
Object Oriented Multithreading Data structures other than Lists Non-LISP macro: LOOP Foreign Function Interface Much much more

Outline
History etc. Core LISP Extending LISP
With Functions With Macros

Conclusions

Macro Definition

Macro Example

Macros vs Functions

Outline
History etc. Core LISP Extending LISP Conclusions: good, bad and ugly

Bottom-Up Design

LISP vs. Fortran

When to Use LISP

When Not to Use LISP

Is LISP a functional language?

Thank You!
References:
Seibel, P. Practical Common Lisp. (2005) First edition.

Você também pode gostar