lisp programming
Download
Report
Transcript lisp programming
Lisp Laboratory
gcLisp (Golden Common Lisp)
The history of Lisp
In summer 1956, Allen Newell, J.C. Shaw, and
Herbert Simon had developed “list processing” and
created IPL (Information Processing Language),
abstract for manipulated symbols and list.
When FORTRAN had build, McCarthy designed a
new languae, LISP ( List Processor), drew on idea
from IPL, FORTRAN and FLPL, run on IBM704
In the 1970s Guy Steele and Gerald Sussman
defined Scheme, combined Algol and Lisp.
Lect. ratchadaporn kanawong
2
The history of Lisp
By the early 1980s, there were dozens of
incompatible Lisp.
A project to define a Common Lisp that would merge
the best feature of existing dialects into a coherent
whole.
The first edition of the Common Lisp standard
appeared in 1984.
Many idea in programming systems such as
interpreted and compiled function, garbage
collection, recursive function calls, source-level
tracing and debugging, and syntax-directed editors.
Lect. ratchadaporn kanawong
3
Installation
Download GC-Lisp from web of course
Double click for unzip file
Extract at “C:\”
Appear dialog, creating folder “C:\GCLisp”
Answer OK
Lect. ratchadaporn kanawong
4
Startup gclisp
Click start bottom and click run and type, or
Double Click Gclisp.exe for loading in
“C:\gclisp” directory
Lect. ratchadaporn kanawong
5
GC-LISP Environment
You will see window of GCLISP as
Lect. ratchadaporn kanawong
6
Some Command:Hot Key
<Alt>-H
<Alt>-E
<Ctrl>-E
<F1>
<F8>
<F9>
<F10>
to get help
to enter the LISP Explorer
to enter the editor
to exit editor into Lisp Environment
to load a file into the editor
to save a file
to save a file as another name
Lect. ratchadaporn kanawong
7
Command line
Type these command follow symbol “*”
Lect. ratchadaporn kanawong
8
Prefix command
All command is in parentheses ( )
It will evaluate when we type “)”
Command form in term prefix
(function <paramete>)
Lect. ratchadaporn kanawong
9
GMAC editor window
Press <Ctrl>-E for getting GMAC editor
<F9> to save file and <F1> back gcLisp
Lect. ratchadaporn kanawong
10
Load file into gclisp
environment
Load first.lsp
* (load ‘first)
Lect. ratchadaporn kanawong
11
Functions and data
The term data means information such as
Numbers
Words
Lists of things
The function operates on the data in some
way and the result is output
Lect. ratchadaporn kanawong
12
Functions Box
Function on Data
data
2
3
Function
+
Lect. ratchadaporn kanawong
result
5
13
A table function in lisp
+
*
/
ABS
Adds two numbers
Subtracts the second from the first
Multiplies two nubmers
Divides the first by the second
Absolute value of a number
Lect. ratchadaporn kanawong
14
SYMBOLs
Symbols are another type of data in Lisp
It is more interesting than numbers
Symbols are named
English words
Phrases
Common abbreviations
Symbol may be letters, number, and special
characters such as hyphen(-)
Lect. ratchadaporn kanawong
15
EXERCISE
Following : symbol or number
AARDVARD
87
1-2-3-GO
3.12
7-11
22/7
-12
Lect. ratchadaporn kanawong
16
THE SPECIAL SYMBOLs
Two Lisp symbols have special meaning
attached to them.
T
Truth, “yes”
NIL
Falsity, emptiness, “no”
***NIL can express by ( )
Lect. ratchadaporn kanawong
17
Some Predicate
A predicate is a question-answering function
(likely yes-or-no question)
Two predicate :
NUMBERP (number predicate) to check whether
expression is number
SYMBOLP (symbol predicate) to check whether
expression is symbol
Lect. ratchadaporn kanawong
18
Test Predicates
Type following this screen
Lect. ratchadaporn kanawong
19
More Predicates
ZEROP
ODDP
EVENP
<
>
EQUAL
NOT
to check zero
to check odd number
to check even number
to compare first < second
to compare first > second
to compare first = second
to opposite
Lect. ratchadaporn kanawong
20
Test Predicate
*
*
*
*
*
*
*
*
*
*
(ZEROP
0)
(ZEROP
5)
(ODDP
1)
(ODDP
2)
(EVENP
1)
(EVENP
2)
(< 2 1)
(> 2 1)
(EQUAL 1 (- 3 2))
(NOT NIL)
Lect. ratchadaporn kanawong
21
Set variable
Binding free variables
Sets variable to the value or the expression
by
(setq variable <expression>)
* (setq x 5)
* (setq a ‘(1 2 3 x y z))
* (setq b ‘(red green blue))
Lect. ratchadaporn kanawong
22
Lists
LISP is named from LISt Processor
Lists are central data type, the most versatile
data type
Lists are important because they can be
made to represent practically anything: sets,
tables, and graphs and even English
sentences.
Lect. ratchadaporn kanawong
23
Example of Lists
(RED
GREEN
BLUE)
NIL
RED
GREEN
BLUE
(2 3 5 7 11 13 17 19)
(Lisp is powerful)
Lect. ratchadaporn kanawong
24
NESTED LISTs
A list may contain other lists as elements
Given the three lists
(BLUE SKY)
(GREEN GRASS)
(BROWN EARTH)
Make list that consists of the above lists
((BLUE SKY) (GREEN GRASS) (BROWN EARTH))
Lect. ratchadaporn kanawong
25
LENGTH OF LISTs
The length of a list is the number of elements
it has
(LENGTH <LIST>)
*
*
*
*
(setq x
(LENGTH
(setq a
(LENGTH
‘(HI MOM))
x)
‘((blue sky) (green grass) (brown earth)))
a)
Lect. ratchadaporn kanawong
26
Exercise
How many elements of following lists have?
(OPEN DOOR PLEASE)
(OPEN (THE DOOR) PLEASE)
((1 2) (2 3) (3 4) (4 5) (5 6))
((ONE) FOR ALL (AND (TWO (FOR ME))))
(A (B C) D ())
(A B (C D) ())
Lect. ratchadaporn kanawong
27
Construct of List
We have three function to make list
(cons <exp> <exp>)
(list <exp> <exp>)
(append <exp> <exp>)
Lect. ratchadaporn kanawong
28
Constructing Lists
Using ‘cons’
(cons ‘(a b) ‘(c d))
((A B) C D)
Using ‘append’
(append ‘(a b) ‘(c d))
(A B C D)
Using ‘list’
(list ‘(a b) ‘(c d))
((A B) (C D))
Lect. ratchadaporn kanawong
29
Make List Example
Type and look the result
Lect. ratchadaporn kanawong
30
Element order in list
In list are considered into two part : head and
tail
a
head
(b c d)
tail
(a b c d)
CAR is function to get head part of list
CDR is function to get tail part of list
Lect. ratchadaporn kanawong
31
Result of car & cdr
* (setq a ‘(x y z))
* (car a)
* (cdr a)
* (setq b ‘((blue sky) (green grass) (brown earth)))
* (car b)
* (cdr b)
* (caar b)
* (cadr b)
* (cdar b)
* (cddr b)
Lect. ratchadaporn kanawong
32
Meaning function c?r
(caar x)
(cadr x)
(cdar x)
(cddr x)
(caadr x)
(caddr x)
(cdddr x)
-
(car (car x))
(car (cdr x))
(cdr (car x))
(cdr (cdr x))
(car (car (cdr x)))
(car (cdr (cdr x)))
(cdr (cdr (cdr x)))
Lect. ratchadaporn kanawong
33
Creating Function
The syntax for creating function as:
(defun <FunctionName> (parameter-list)
<function body>)
Lect. ratchadaporn kanawong
34
Example Function
Look this example
(defun call-up (caller callee)
(list ‘hello callee ‘this ‘is caller ‘calling))
* (call-up ‘FRED ‘WANDA)
Lect. ratchadaporn kanawong
35
Example Function
Look this example
(defun double (n)
(* n 2))
* (double 2)
Lect. ratchadaporn kanawong
36
Example Function
(defun absolute-value(x)
(cond ((< x 0) (- x))
((>= x 0) x)))
(defun absolute-value(x)
(cond ((< x 0) (- x))
(t
x)))
(defun absolute-value(x)
(if (< x 0) (- x) x))
* (absolute-value 2)
Lect. ratchadaporn kanawong
37
Conditionals(1)
Its form as:
(cond
(<condition1> <action1>)
(<condition2> <action2>)
……..
(<conditionN> <actionN>))
Lect. ratchadaporn kanawong
38
Conditionals
Its form as:
(if <test> <then-action> [<else-action>])
Some predicates for condition test:
= > < >= <=
oddp evenp numberp minusp zerop plusp
member atom listp null equal and or not
Lect. ratchadaporn kanawong
39
Recursive Functions
(defun my-member(element list)
(cond ((null list) nil)
((equal element (car list)) list)
(t (my-member element (cdr list)))))
(defun my-length(list)
(cond ((null list) 0)
(t (+ (my-length(cdr list)) 1))))
Lect. ratchadaporn kanawong
40
Recursive Functions
(defun count-atoms(list)
(cond ((null list) 0)
((atom list) 1)
(t (+ (count-atoms (car list))
(count-atoms (cdr list))))))
Lect. ratchadaporn kanawong
41
Linear or cdr recursion
(length ((1 2) 3 (1 (4 (5)))))
3
+
1
(length (3 (1 (4 (5)))))
+
1
(length ((1 (4 (5)))))
+
1
(length ())
Lect. ratchadaporn kanawong
0
42
Tree of recursion
Lect. ratchadaporn kanawong
43
Side Effects
*(defun f(x)
(setq inc (+ inc 1))
(+ x inc))
*(setq inc 0)
*(f 4)
*(f 4)
*inc
*x
*(defun foo(x)
(setq x (+ x 1))
x)
*(setq y 1)
*(foo y)
*(foo y)
*y
*x
Lect. ratchadaporn kanawong
44