440 likes | 585 Views
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.
E N D
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
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
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
Startup gclisp • Click start bottom and click run and type, or • Double Click Gclisp.exe for loading in “C:\gclisp” directory Lect. ratchadaporn kanawong
GC-LISP Environment • You will see window of GCLISP as Lect. ratchadaporn kanawong
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
Command line • Type these command follow symbol “*” Lect. ratchadaporn kanawong
Prefix command • All command is in parentheses ( ) • It will evaluate when we type “)” • Command form in term prefix (function <paramete>) Lect. ratchadaporn kanawong
GMAC editor window • Press <Ctrl>-E for getting GMAC editor • <F9> to save file and <F1> back gcLisp Lect. ratchadaporn kanawong
Load file into gclisp environment • Load first.lsp * (load ‘first) Lect. ratchadaporn kanawong
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
Functions Box • Function on Data Function data result 2 + 5 3 Lect. ratchadaporn kanawong
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
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
EXERCISE • Following : symbol or number • AARDVARD • 87 • 1-2-3-GO • 3.12 • 7-11 • 22/7 • -12 Lect. ratchadaporn kanawong
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
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
Test Predicates • Type following this screen Lect. ratchadaporn kanawong
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
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
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
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
Example of Lists (RED GREEN BLUE) (2 3 5 7 11 13 17 19) (Lisp is powerful) NIL RED GREEN BLUE Lect. ratchadaporn kanawong
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
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
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
Construct of List • We have three function to make list (cons <exp> <exp>) (list <exp> <exp>) (append <exp> <exp>) Lect. ratchadaporn kanawong
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
Make List Example • Type and look the result Lect. ratchadaporn kanawong
Element order in list • In list are considered into two part : head and tail a head (a b c d) tail (b c d) CAR is function to get head part of list CDR is function to get tail part of list Lect. ratchadaporn kanawong
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
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
Creating Function The syntax for creating function as: (defun <FunctionName> (parameter-list) <function body>) Lect. ratchadaporn kanawong
Example Function Look this example (defun call-up (caller callee) (list ‘hello callee ‘this ‘is caller ‘calling)) * (call-up ‘FRED ‘WANDA) Lect. ratchadaporn kanawong
Example Function Look this example (defun double (n) (* n 2)) * (double 2) Lect. ratchadaporn kanawong
Example Function (defun absolute-value(x) (cond ((< x 0) (- x)) ((>= x 0) x))) (defun absolute-value(x) (if (< x 0) (- x) x)) (defun absolute-value(x) (cond ((< x 0) (- x)) (t x))) * (absolute-value 2) Lect. ratchadaporn kanawong
Conditionals(1) • Its form as: (cond (<condition1> <action1>) (<condition2> <action2>) …….. (<conditionN> <actionN>)) Lect. ratchadaporn kanawong
Conditionals • Its form as: • Some predicates for condition test: (if <test> <then-action> [<else-action>]) = > < >= <= oddp evenp numberp minusp zerop plusp member atom listp null equal and or not Lect. ratchadaporn kanawong
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
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
+ + + 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 ()) 0 Lect. ratchadaporn kanawong
Tree of recursion Lect. ratchadaporn kanawong
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