a little lisp

42
(A Little LISP) By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham All images © iStockPhoto or Wikipedia

Upload: melbournepatterns

Post on 11-Nov-2014

3.556 views

Category:

Technology


1 download

DESCRIPTION

Presentation on Lisp given by James Ladd to the Melbourne Patterns group in September 2008

TRANSCRIPT

Page 1: A Little Lisp

(A Little LISP)

By James Ladd http://www.jamesladdcode.com

with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Page 2: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

WHAT ?

Page 3: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

The greatest single

programming language ever

designed.

— Alan Kay

Page 4: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

John McCarthy

Created LISP

Page 5: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

John McCarthy

Recursive Functions

of Symbolic

Expressions and Their

Computation by Machine

Page 6: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

John McCarthy

In 1958

Created LISP

Page 7: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

LISP <( )=

Page 8: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

In 1958

Page 9: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

1958

Page 10: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

In 1958

Page 11: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

In 1958

C IBM 704 FORTRAN

C AREA OF A TRIANGLE WITH A STANDARD SQUARE ROOT FUNCTION C INPUT - CARD READER UNIT 5, INTEGER INPUTC OUTPUT - LINE PRINTER UNIT 6, REAL OUTPUTC INPUT ERROR DISPLAY ERROR OUTPUT CODE 1 IN JOB CONTROL LISTING READ INPUT TAPE 5, 501, IA, IB, IC 501 FORMAT (3I5)C IA, IB, AND IC MAY NOT BE NEGATIVEC FURTHERMORE, THE SUM OF TWO SIDES OF A TRIANGLEC IS GREATER THAN THE THIRD SIDE, SO WE CHECK FOR THAT, TOO IF (IA) 777, 777, 701 701 IF (IB) 777, 777, 702 702 IF (IC) 777, 777, 703 703 IF (IA+IB-IC) 777,777,704 704 IF (IA+IC-IB) 777,777,705 705 IF (IB+IC-IA) 777,777,799 777 STOP 1C USING HERON'S FORMULA WE CALCULATE THEC AREA OF THE TRIANGLE 799 S = FLOATF (IA + IB + IC) / 2.0 AREA = SQRT( S * (S - FLOATF(IA)) * (S - FLOATF(IB)) * + (S - FLOATF(IC))) WRITE OUTPUT TAPE 6, 601, IA, IB, IC, AREA 601 FORMAT (4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA= ,F10.2, + 13H SQUARE UNITS) STOP END

Page 12: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Created LISP as

mathematical notation for

computer programs

Page 13: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Based on Lambda Calculus

Page 14: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Lambda Calculus

the function f(x, y) = x - y

would be written as λ x. λ y.

x - y.

Page 15: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Page 16: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

List Processing Language

(FOO (BAR 1) 2)

Page 17: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

LISP pioneered:

Tree Data Structures

Automatic Storage

Management

Dynamic Typing

Object Oriented Programming

Page 18: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

LISP pioneered:

Self-hosting Compiler !!

Page 19: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

LISP = Pure Functional

Language

Page 20: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

FIFTY YEARS AGO

Page 21: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Functional Programs Are:

Stateless

Deal only with Functions

No side effects in 'state'

Functions can return

Functions

Page 22: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(print "Hello

world")

Page 23: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(defun factorial (n)

(if (<= n 1)

1

(* n (factorial (- n

1)))))

Page 24: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(dotimes (i 10)

(format t "~A~%" (+ 1

i)))

Page 25: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

LISP is a series of expressions

foo

()

(foo)

(foo bar)

(a b (c) d)

Page 26: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

A LISP expression

Atom or a list of zero or more

expressions, separated by

whitespace and enclosed in ().

Page 27: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Foo = atom

() = empty list

(foo) = list of one atom

(foo bar) = list of two atoms

(a b (c) d) = list of 4 elements,

the 3rd one is itself a

list.

Page 28: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

> (+ 1 2)

3

>

Page 29: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

> (+ 1 (- 10 5))

6

>

Page 30: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(quote x) returns

x

>(quote a)

a

>'a

a

>(quote (+ 1 2))

(+ 1 2)

Page 31: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(atom x) returns t if

x

is an atom,

otherwise ()

>(atom 'a)

t

>(atom '(a b c))

()

Page 32: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(eq x y) returns t if x and y

are same atom or both (),

otherwise ()

>(eq 'a 'a) >(eq '() '())

t t

>(eq 'a 'b)

()

Page 33: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(car x) returns first

element of

value of x

>(car '(a b c))

a

>(car '('(foo) bar baz))

(foo)

Page 34: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(cdr x) returns everything

after the first element of

value of x

>(cdr '(a b c))

(b c)

>(cdr '('(foo) bar baz))

(bar baz)

Page 35: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(cons x y) returns a list

containing value of x

followed by value of y

>(cons 'a '(b c))

(a b c)

>(car (cons 'a '(b c)))

(b c)

Page 36: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Page 37: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(cond (p1 e1)...(pn en))

The p expressions are evaluated

in order until one returns t.

When one is found, the value of

the corresponding e expression

is returned as the value of the

whole cond expression.

Page 38: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(cond (p1 e1)...(pn en))

>(cond ((eq 'a 'b) 'first)

((atom 'a) 'second))

second

Page 39: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

(cond (p1 e1)...(pn en))

(cond (x y) ('t z))

is equivalent to

if x then y else z

Page 40: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Recommended Reading

The Little Lisper

ANSI Common Lisp

Page 41: A Little Lisp

A Little LISP, By James Ladd http://www.jamesladdcode.com with portions from The Roots Of Lisp, by Paul Graham

All images © iStockPhoto or Wikipedia

Some real code ...

Page 42: A Little Lisp