200 likes | 385 Views
C-LISP. LISP. Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT). McCarthy published its design in a paper in Communications of the ACM in 1960, entitled "Recursive Functions of Symbolic Expressions and Their Computation by Machine,.
E N D
LISP Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT). McCarthy published its design in a paper in Communications of the ACM in 1960, entitled "Recursive Functions of Symbolic Expressions and Their Computation by Machine,
The term “Lisp” itself originally stood for “LISt Processing.” Significant Language Features • Atoms & Lists - Lisp uses two different types of data structures, atoms and lists. • Atoms are similar to identifiers, but can also be numeric constants • Lists can be lists of atoms, lists, or any combination of the two • Functional Programming Style - all computation is performed by applying functions to arguments. Variable declarations are rarely used. • Uniform Representation of Data and Code - example: the list (A B C D) • a list of four elements (interpreted as data) • Reliance on Recursion - a strong reliance on recursion has allowed Lisp to be successful in many areas, including Artificial Intelligence. • Garbage Collection - Lisp has built-in garbage collection, so programmers do not need to explicitly free dynamically allocated memory.
Advantages of LISP • Recursion: • A program can call itself a sub routine • Garbage Collector: • Data storage is automatically recycled • Uniform Representation: • Program and data look the same • Program can execution other program • Interaction: • User can combine program writing, compilation,testing,debugging,running in a single interacting session
Application of LISP • Artificial Intelligence • Symbolical Algebraic Manipulation • Natural Language Understanding • Machine Translation • Expert System • Diagnosis System • Automatic Programming (Robotics) • Perception(Vision, Speech Understanding)
Syntax: • Prefix notation – Operator first, arguments follow – E.g. (+ 3 2) adds 3 and 2 A lot of parentheses • These define lists and also programs • Examples: – (a b c d) is a list of 4 elements (atoms) a,b,c,d – (defun factorial (num) (cond ((<= num 0) 1) (t (* (factorial (- num 1)) num)) )) LISP Syntax
LISP Data Types There are two: atoms and lists. • Atoms: • Integer, Float ,String, Character, symbol, Boolean etc • a, 7, tom, my-age, nil, T • – Evaluate an atom gives the value assigned that atom. • Numbers are special--they always evaluate to themselves. • (setq my-age 24) => 24 • my-age => 24 • – Reserved: nil and T • nil => nil • t => t • – Case insensitive: aBc and ABC are the same atom
2 Data Types: Lists Lists: (a), (+ 6 7), (a (e f g) h), (), nil – Evaluating a list always invokes a function. (function-name arg1 … argn) (+ 6 7) => 13 (foo 17 18 19) => Error (function not defined) (+ my-age 4) => 28 • Lists are chains of pairs • Can make other trees, etc as well
Definition of a function (defun <name> <documentation-string> (<arguments>) <body>) (defun square “computes square” (x) (* x x)) function definition Lisp includes many built-in functions: +, *, -, /, max, min, sqrt Once a list is built, how do we access its members? – first and car give you the first element of a list. (first ‘(1 2 3)) => 1 (first ‘((a b) 2 3)) => (a b) (length ‘(a b c)) => 3
Many ways to define iterations Commands: • loop • dolist • dotimes • do, do* Iterations: loop > (setq a 4) 4 > (loop (setq a (+ a 1)) (when (> a 7) (return a))) ;; return exists the loop 8 > (loop (setq a (- a 1)) (when (< a 3) (return))) NIL
Many ways to define iterations Commands: • loop • dolist • dotimes • do, do* Iterations: loop > (setq a 4) 4 > (loop (setq a (+ a 1)) (when (> a 7) (return a))) ;; return exists the loop 8 > (loop (setq a (- a 1)) (when (< a 3) (return))) NIL
Iterations: dolist > (dolist (x '(1 2 3 4)) (print x)) 1 2 3 4 NIL ;; NIL is returned by dolist Iterations: dotimes > (dotimes (i 4) (print i)) ;; starts from 0 and continues till limit 4 0 1 2 3 4 NIL ;; returns NIL
Basic Functions • CAR returns the head of a list • CDR returns the tail of a list • CONS inserts a new head into a list • EQ compares two atoms for equality • ATOM tests if its argument is an atom
Other useful Functions • (NULL S) tests if S is the empty list • (LISTP S) tests if S is a list • LIST makes a list of its (evaluated) arguments • (LIST 'A '(B C) 'D) returns (A (B C) D) • (LIST (CDR '(A B)) 'C) returns ((B) C) • APPEND concatenates two lists • (APPEND '(A B) '((X) Y) ) returns (A B (X) Y)
CAR • The CAR of a list is the first thing in the list • CAR is only defined for nonempty lists If L isThen (CAR L) is (A B C) A ( (X Y) Z) (X Y) ( ( ) ( ) ) ( ) ( )undefined
CDR examples If L isThen (CDR L) is (A B C) (B C) ( (X Y) Z) (Z) (X) ( ) ( ( ) ( ) ) ( ( ) ) ( )undefined First Program (defun hello () (write-string "Hello, World!"))
Lab • Write Program to display following output 1 x 2 = 2 2 x 2 = 4 2 x 3 = 8 • What is wrong with each of the following function definitions? • (defun speak (x y) (list ’all ’x ’is ’y)) • (defun speak (x) (y) (list ’all x ’is y)) • (defun speak ((x) (y)) (list all ’x is ’y)) • Here is an example of the function MYFUN, a strange function of two inputs. (myfun ’alpha ’beta) => ((ALPHA) BETA) Write MYFUN. Test your function to make certain it works correctly.