Presentation is loading. Please wait.

Presentation is loading. Please wait.

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.

Similar presentations


Presentation on theme: "C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute."— Presentation transcript:

1 C-LISP

2 LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute of Technology 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, Communications of the ACM

3 Significant Language Features 3  The term “Lisp” itself originally stood for “LISt Processing.” 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.

4 Advantages of LISP 4 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

5 Application of LISP 5 Artificial Intelligence Symbolical Algebraic Manipulation Natural Language Understanding Machine Translation Expert System Diagnosis System Automatic Programming (Robotics) Perception(Vision, Speech Understanding)

6 LISP Syntax 6 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)) ))

7 LISP Data Types 7 There are two: atoms and lists. 1Atoms: 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

8 8 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

9 LISP Expressions 9 Numeric FunctionsMeaning (+ x 1 x 2... x n ) The sum of x 1, x 2,..., x n (* x 1 x 2... x n ) The product of x 1, x 2,..., x n (- x y) Subtract y from x (/ x y) Divide x by y (rem x y) The remainder of dividing x by y (abs x) The absolute value of x (max x 1 x 2... x n ) The maximum of x 1, x 2,..., x n (min x 1 x 2... x n ) The minimum of x 1, x 2,..., x n

10 function definition 10 Definition of a function (defun ( ) ) (defun square “computes square” (x) (* x x)) 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

11 Relational Operators 11 Relational OperatorsMeaning (= x y) x is equal to y (/= x y) x is not equal to y (< x y) x is less than y (> x y) x is greater than y (<= x y) x is no greater than y (>= x y) x is no less than y

12 Logical Operators 12 Logical OperatorsMeaning (or x 1 x 2... x n ) Logical or (and x 1 x 2... x n ) Logical and (not x) Logical negation

13 13 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

14 14 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

15 15 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

16 16 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

17 17 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)

18 18 CAR The CAR of a list is the first thing in the list CAR is only defined for nonempty lists If L is Then (CAR L) is (A B C)A ( (X Y) Z)(X Y) ( ( ) ( ) )( ) ( ) undefined

19 19 CDR examples If L is Then (CDR L) is (A B C)(B C) ( (X Y) Z)(Z) ( ( ) ( ) )( ( ) ) ( ) undefined (X)( ) (defun hello () (write-string "Hello, World!")) First Program

20 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. 20


Download ppt "C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute."

Similar presentations


Ads by Google