a crash course in lisp - ntnu5/14 23l23m3322? i ˚ p symbol manipulation everything in a computer is...

106
1/14 P i ? 22 33 3 M L 2 3 2 Department of Computer and information Science Norwegian University of Science and Technology http://www.idi.ntnu.no/ A Crash Course in LISP MNFIT272 – 2002 Anders Kofod-Petersen [email protected]

Upload: others

Post on 23-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

1/14 P�i?22333ML232

Department of Computer and information ScienceNorwegian University of Science and Technologyhttp://www.idi.ntnu.no/

A Crash Course in LISPMNFIT272 – 2002

Anders Kofod-Petersen

[email protected]

Page 2: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

Page 3: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

Page 4: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

Page 5: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

Page 6: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

Page 7: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

– Functions

Page 8: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

– Functions

– Recursion

Page 9: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

– Functions

– Recursion

– Assignment

Page 10: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

– Functions

– Recursion

– Assignment

– Functional programming

Page 11: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

2/14 P�i?22333ML232

Introduction

What are the topics for this crash course?

• Overview

• Symbol manipulation

• LISP

– Data

– Functions

– Recursion

– Assignment

– Functional programming

– Iteration

Page 12: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

3/14 P�i?22333ML232

Overview

Page 13: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

3/14 P�i?22333ML232

Overview

• LISP takes its name from List Proccessing, some argue that it comes from Lots

of Irritating Silly Parentheses

Page 14: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

3/14 P�i?22333ML232

Overview• LISP takes its name from List Proccessing, some argue that it comes from Lots

of Irritating Silly Parentheses

• LISP has the easiest syntax of all languages

Page 15: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

3/14 P�i?22333ML232

Overview• LISP takes its name from List Proccessing, some argue that it comes from Lots

of Irritating Silly Parentheses

• LISP has the easiest syntax of all languages

• LISP is oriented towards the manipulation of symbols

Page 16: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

3/14 P�i?22333ML232

Overview• LISP takes its name from List Proccessing, some argue that it comes from Lots

of Irritating Silly Parentheses

• LISP has the easiest syntax of all languages

• LISP is oriented towards the manipulation of symbols

• LISP is very flexible in that users can change the syntax of the language if they

don’t like it

Page 17: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools

Page 18: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools

• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

Page 19: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

Page 20: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools

• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

(defun sum (n)

(let ((s 0))

(dotimes (i n s)

(incf s i))))

Page 21: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

(defun sum (n)

(let ((s 0))

(dotimes (i n s)

(incf s i))))

int sum(int n){int i,s=0;

for(1=0;i<n;i++){s+=i;}

return(s);

}

Page 22: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools

• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

(defun sum (n)

(let ((s 0))

(dotimes (i n s)

(incf s i))))

int sum(int n){int i,s=0;

for(1=0;i<n;i++){s+=i;}

return(s);

}

• To write a function that takes a number n and returns a function that adds n n

to its arguments:

Page 23: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools

• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

(defun sum (n)

(let ((s 0))

(dotimes (i n s)

(incf s i))))

int sum(int n){int i,s=0;

for(1=0;i<n;i++){s+=i;}

return(s);

}

• To write a function that takes a number n and returns a function that adds n n

to its arguments:

(defun addn (n)

#’(lambda (x)

(+ x n)))

Page 24: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

4/14 P�i?22333ML232

New tools• You can do things in LISP that can’t be done in “ordinary” programming lan-

guages

• To write a function that returns the sum:

(defun sum (n)

(let ((s 0))

(dotimes (i n s)

(incf s i))))

int sum(int n){int i,s=0;

for(1=0;i<n;i++){s+=i;}

return(s);

}

• To write a function that takes a number n and returns a function that adds n n

to its arguments:

(defun addn (n)

#’(lambda (x)

(+ x n)))

Sorry, no can do!

Page 25: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

Page 26: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

Page 27: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

Page 28: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

Page 29: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

Page 30: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

• Groups of atoms can form sentence like objects called lists

Page 31: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

• Groups of atoms can form sentence like objects called lists

– Examples are: (1 2 3) (cow horse (hog pig))

Page 32: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

• Groups of atoms can form sentence like objects called lists

– Examples are: (1 2 3) (cow horse (hog pig))

• Atoms and lists are collectively called symbolic expressions

Page 33: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

• Groups of atoms can form sentence like objects called lists

– Examples are: (1 2 3) (cow horse (hog pig))

• Atoms and lists are collectively called symbolic expressions

• Programs and data are represented the same way

Page 34: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

5/14 P�i?22333ML232

Symbol Manipulation

• Everything in a computer is a string of bits

• Bits can be interpreted as a code for word like objects

• The fundamental things formed from bits in LISP are atoms

– Examples are: 3.1416, This-is-an-atom, nil

• Groups of atoms can form sentence like objects called lists

– Examples are: (1 2 3) (cow horse (hog pig))

• Atoms and lists are collectively called symbolic expressions

• Programs and data are represented the same way

– is (+ 10 15) a program or data?

Page 35: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

Page 36: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

Page 37: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

Page 38: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

Page 39: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

Page 40: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

Page 41: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

Page 42: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

• Lists are build by using cons:

Page 43: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

• Lists are build by using cons:

– (cons ’a ’(b c d)) -- (a b c d)

Page 44: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

• Lists are build by using cons:

– (cons ’a ’(b c d)) -- (a b c d)

• Lists are manipulated by using car and cdr:

Page 45: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

• Lists are build by using cons:

– (cons ’a ’(b c d)) -- (a b c d)

• Lists are manipulated by using car and cdr:

– (car ’(a b c)) -- a

Page 46: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

6/14 P�i?22333ML232

LISP

Data

• LISP offers all the data types that other languages do

• “New” data types are:

– Symbols – ’LISP

– Lists – ’(LISP has 2 new types)

• You can build lists by list:

– (list ’LISP ’has (+ 1 1) ’new ‘‘types’’)

• Lists are build by using cons:

– (cons ’a ’(b c d)) -- (a b c d)

• Lists are manipulated by using car and cdr:

– (car ’(a b c)) -- a

– (cdr ’(a b c)) -- (c d)

Page 47: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

Page 48: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

Page 49: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

Page 50: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

Page 51: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

Page 52: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

Page 53: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

– (defun square (x)

(* x x))

Page 54: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

– (defun square (x)

(* x x))

– Notice that LISP uses prefix and not infix notation

Page 55: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

– (defun square (x)

(* x x))

– Notice that LISP uses prefix and not infix notation

– LISP makes no distinction between a program, a procedure, or a function –

Functions do for everything

Page 56: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

– (defun square (x)

(* x x))

– Notice that LISP uses prefix and not infix notation

– LISP makes no distinction between a program, a procedure, or a function –

Functions do for everything

• You can also read and write input and output

Page 57: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

7/14 P�i?22333ML232

Functions

• You can define new functions with defun

– Defun takes three or more arguments:

1. A name

2. A list of parameters

3. One or more expressions that makes the body of the function

– (defun square (x)

(* x x))

– Notice that LISP uses prefix and not infix notation

– LISP makes no distinction between a program, a procedure, or a function –

Functions do for everything

• You can also read and write input and output

– (defun ask (string)

(format t ‘‘ A’’ string)

(read))

Page 58: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

Page 59: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

Page 60: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

Page 61: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

Page 62: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

Page 63: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

Page 64: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

(defun len (lst)

(if (null lst) 0

(+ (len (cdr lst)) 1)))

Page 65: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

(defun len (lst)

(if (null lst) 0

(+ (len (cdr lst)) 1)))

> (len ’(a b c d))

4

Page 66: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

(defun len (lst)

(if (null lst) 0

(+ (len (cdr lst)) 1)))

> (len ’(a b c d))

4

– Recursive functions can be used for more complex problems:

Page 67: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

(defun len (lst)

(if (null lst) 0

(+ (len (cdr lst)) 1)))

> (len ’(a b c d))

4

– Recursive functions can be used for more complex problems: (defun fibonacci

(x)

(if (<= x 2) 1

(+ (fibonacci (- x 2))(fibonacci (1- x)))))

Page 68: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

8/14 P�i?22333ML232

Recursion

• Recursion is a big thing in LISP

– When writing recursive functions two issues are paramount:

1. Handle the trivial case first

2. Then handle the recursive case

– Defining a function that calculates the length of a list:

(defun len (lst)

(if (null lst) 0

(+ (len (cdr lst)) 1)))

> (len ’(a b c d))

4

– Recursive functions can be used for more complex problems: (defun fibonacci

(x)

(if (<= x 2) 1

(+ (fibonacci (- x 2))(fibonacci (1- x))))) > (fibonacci

10)

55

Page 69: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

Page 70: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

Page 71: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

Page 72: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

2. Using the let construction

Page 73: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

2. Using the let construction

(defun showvar (x y)

(setf var x)

(let ((var y))

(print var))

(print var)

t)

Page 74: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

2. Using the let construction

(defun showvar (x y)

(setf var x)

(let ((var y))

(print var))

(print var)

t)

> (showvar 10 5)

5

10

t

Page 75: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

2. Using the let construction

(defun showvar (x y)

(setf var x)

(let ((var y))

(print var))

(print var)

t)

> (showvar 10 5)

5

10

t

• For global parameters use defparameter or defconstant

Page 76: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

9/14 P�i?22333ML232

Variables

• There are several ways of defining variables in LISP:

1. Using the setf construction

2. Using the let construction

(defun showvar (x y)

(setf var x)

(let ((var y))

(print var))

(print var)

t)

> (showvar 10 5)

5

10

t

• For global parameters use defparameter or defconstant

– (defparameter *global* 100)

Page 77: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

Page 78: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

Page 79: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

Page 80: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

• > (remove ’l lst)

(H e o)

Page 81: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

• > (remove ’l lst)

(H e o)

• > lst

(H e l l o)

Page 82: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

• > (remove ’l lst)

(H e o)

• > lst

(H e l l o)

• The most important advantage with this non-destructive way of writing, is the

possibility if testing each function as you write them, and guarantee that they

are correct

Page 83: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

• > (remove ’l lst)

(H e o)

• > lst

(H e l l o)

• The most important advantage with this non-destructive way of writing, is the

possibility if testing each function as you write them, and guarantee that they

are correct

• If you want to write destructive code you can do:

Page 84: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

10/14 P�i?22333ML232

Functional programming

• Functional programming means writing functions that returns values

• > (setf lst ’(H e l l o))

• > (remove ’l lst)

(H e o)

• > lst

(H e l l o)

• The most important advantage with this non-destructive way of writing, is the

possibility if testing each function as you write them, and guarantee that they

are correct

• If you want to write destructive code you can do:

> (setf lst (remove ’l lst))

Page 85: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

Page 86: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

Page 87: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

Page 88: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

Page 89: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

> (lenit ’(a b c d))

4

Page 90: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

> (lenit ’(a b c d))

4

• dolist is not the only interactive function, others are: do, dotimes, and loop

Page 91: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

> (lenit ’(a b c d))

4

• dolist is not the only interactive function, others are: do, dotimes, and loop

• For handling batch like coding, LISP offers constructions like progn

Page 92: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

> (lenit ’(a b c d))

4

• dolist is not the only interactive function, others are: do, dotimes, and loop

• For handling batch like coding, LISP offers constructions like progn

(defun write2 (lst)

(progn (print lst)

(print (reverse lst))))

Page 93: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

12/14 P�i?22333ML232

Page 94: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

11/14 P�i?22333ML232

Iteration

• It can sometimes be more natural repeat a procedure in an interactive way, as

compared to a recursive way

• Redefining the length function:

(defun lenit (lst)

(let ((len 0))

(dolist (obj lst) (setf len (+ len 1))) len))

> (lenit ’(a b c d))

4

• dolist is not the only interactive function, others are: do, dotimes, and loop

• For handling batch like coding, LISP offers constructions like progn

(defun write2 (lst)

(progn (print lst)

(print (reverse lst))))

Page 95: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

12/14 P�i?22333ML232

> (write2 ’(a b c d e))

(a b c b a)

(e d c b a)

Page 96: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI

Page 97: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI

• The LISP used at IDI is Allegro Common LISP; a de facto standard

Page 98: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

Page 99: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI

• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

Page 100: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

Page 101: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI

• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

1. Open an emacs

Page 102: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

1. Open an emacs

2. Type <esc>-x

fi:commmon-lisp

Page 103: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

1. Open an emacs

2. Type <esc>-x

fi:commmon-lisp

3. Make a new frame

Page 104: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI

• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

1. Open an emacs

2. Type <esc>-x

fi:commmon-lisp

3. Make a new frame

4. type <esc>-x

lisp-mode

Page 105: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

13/14 P�i?22333ML232

LISP at IDI• The LISP used at IDI is Allegro Common LISP; a de facto standard

• To get LISP to work in emacs, put the following into your .emacs file:

(setq inferior-lisp-program "/local/acl/mlisp")

(load "/usr/local/acl/eli/fi-site-init.el")

• Notice that emacs is controlled with LISP!

1. Open an emacs

2. Type <esc>-x

fi:commmon-lisp

3. Make a new frame

4. type <esc>-x

lisp-mode

5. You can now write your lisp code in one window, and compile it with <ctrl>-x

Page 106: A Crash Course in LISP - NTNU5/14 23L23M3322? i ˚ P Symbol Manipulation Everything in a computer is a string of bits Bits can be interpreted as a code for word like objects The fundamental

14/14 P�i?22333ML232

Litterature

• LISP 3rd edition

Patrick Henry Winston

Berthold Klaus Paul Horn

ISBN: 0-201-08379-1

• ANSI Common Lisp

Paul Graham

ISBN: 0-13-370875-6

• Common Lisp the Language, 2nd edition

Guy L. Steel

ISBN: 1-55558-041-6

http://www.math.uio.no/cltl/cltl2.html