lisp laboratory
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Lisp Laboratory
gcLisp (Golden Common Lisp)
Lect. ratchadaporn kanawong 2
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 3
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 4
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 5
Startup gclisp
Click start bottom and click run and type, or
Double Click Gclisp.exe for loading in “C:\gclisp” directory
Lect. ratchadaporn kanawong 6
GC-LISP Environment
You will see window of GCLISP as
Lect. ratchadaporn kanawong 7
Some Command:Hot Key
<Alt>-H to get help <Alt>-E to enter the LISP Explorer <Ctrl>-E to enter the editor <F1> to exit editor into Lisp Environment <F8> to load a file into the editor <F9> to save a file <F10> to save a file as another name
Lect. ratchadaporn kanawong 8
Command line
Type these command follow symbol “*”
Lect. ratchadaporn kanawong 9
Prefix command
All command is in parentheses ( ) It will evaluate when we type “)” Command form in term prefix
(function <paramete>)
Lect. ratchadaporn kanawong 10
GMAC editor window
Press <Ctrl>-E for getting GMAC editor
<F9> to save file and <F1> back gcLisp
Lect. ratchadaporn kanawong 11
Load file into gclisp environment
Load first.lsp
* (load ‘first)* (load ‘first)
Lect. ratchadaporn kanawong 12
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 13
Functions Box
Function on Data
Functiondata result
+2
53
Lect. ratchadaporn kanawong 14
A table function in lisp
+ Adds two numbers
- Subtracts the second from the first
* Multiplies two nubmers
/ Divides the first by the second
ABS Absolute value of a number
Lect. ratchadaporn kanawong 15
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 16
EXERCISE
Following : symbol or number AARDVARD 87 1-2-3-GO 3.12 7-11 22/7 -12
Lect. ratchadaporn kanawong 17
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 18
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 19
Test Predicates
Type following this screen
Lect. ratchadaporn kanawong 20
More Predicates
ZEROP to check zero
ODDP to check odd number
EVENP to check even number
< to compare first < second
> to compare first > second
EQUAL to compare first = second
NOT to opposite
Lect. ratchadaporn kanawong 21
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 22
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 23
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 24
Example of Lists
(RED GREEN BLUE)
(2 3 5 7 11 13 17 19)
(Lisp is powerful)
RED GREEN BLUE
NIL
Lect. ratchadaporn kanawong 25
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 26
LENGTH OF LISTs
The length of a list is the number of elements it has
(LENGTH <LIST>)
* (setq x ‘(HI MOM))* (LENGTH x)* (setq a ‘((blue sky) (green grass) (brown earth)))* (LENGTH a)
Lect. ratchadaporn kanawong 27
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 28
Construct of List
We have three function to make list
(cons <exp> <exp>)
(list <exp> <exp>)
(append <exp> <exp>)
Lect. ratchadaporn kanawong 29
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 30
Make List Example
Type and look the result
Lect. ratchadaporn kanawong 31
Element order in list
In list are considered into two part : head and tail
(a b c d)
(b c d)
a head
tail
CAR is function to get head part of listCDR is function to get tail part of list
Lect. ratchadaporn kanawong 32
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 33
Meaning function c?r
(caar x) - (car (car x))
(cadr x) - (car (cdr x))
(cdar x) - (cdr (car x))
(cddr x) - (cdr (cdr x))
(caadr x) - (car (car (cdr x)))
(caddr x) - (car (cdr (cdr x)))
(cdddr x) - (cdr (cdr (cdr x)))
Lect. ratchadaporn kanawong 34
Creating Function
The syntax for creating function as:
(defun <FunctionName> (parameter-list)
<function body>)
Lect. ratchadaporn kanawong 35
Example Function
Look this example
(defun call-up (caller callee)
(list ‘hello callee ‘this ‘is caller ‘calling))
* (call-up ‘FRED ‘WANDA)
Lect. ratchadaporn kanawong 36
Example Function
Look this example
(defun double (n)
(* n 2))
* (double 2)
Lect. ratchadaporn kanawong 37
Example Function
(defun absolute-value(x)
(cond ((< x 0) (- x))
((>= x 0) x)))
* (absolute-value 2)
(defun absolute-value(x)
(cond ((< x 0) (- x))
(t x)))
(defun absolute-value(x)
(if (< x 0) (- x) x))
Lect. ratchadaporn kanawong 38
Conditionals(1)
Its form as:
(cond (<condition1> <action1>) (<condition2> <action2>) …….. (<conditionN> <actionN>))
Lect. ratchadaporn kanawong 39
Conditionals
Its form as:
Some predicates for condition test:
(if <test> <then-action> [<else-action>])
= > < >= <=oddp evenp numberp minusp zerop pluspmember atom listp null equal and or not
Lect. ratchadaporn kanawong 40
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 41
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 42
Linear or cdr recursion
(length ((1 2) 3 (1 (4 (5)))))
(length (3 (1 (4 (5)))))
(length ((1 (4 (5)))))
(length ())
1
1
1
0
+
+
+
3
Lect. ratchadaporn kanawong 43
Tree of recursion
Lect. ratchadaporn kanawong 44
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