Presentation is loading. Please wait.

Presentation is loading. Please wait.

EZGİ GENÇ 07104405. History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally.

Similar presentations


Presentation on theme: "EZGİ GENÇ 07104405. History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally."— Presentation transcript:

1 EZGİ GENÇ 07104405

2 History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958 by John McCarthy, Lisp is the second- oldest high-level programming language in widespread use today; only Fortran is older. Like Fortran, Lisp has changed a great deal since its early days, and a number of dialects have existed over its history. Today, the most widely known general-purpose Lisp dialects are Common Lisp and Scheme. LISP came from `LISt Processing language', but mythically from `Lots of Irritating Superfluous Parentheses‘..

3 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. 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 Areas of Application Lisp totally dominated Artificial Intelligence applications for a quarter of a century, and is still the most widely used language for AI. In addition to its success in AI, Lisp pioneered the process of Functional Programming. Below is a list of the areas where Lisp has been used: Artificial Intelligence AI Robots Computer Games Pattern Recognition Air Defense Systems Implementation of Real-Time, embedded Knowledge-Based Systems List Handling and Processing Tree Traversal (Breath/Depth First Search)

5 Syntax: Prefix notation – Operator first, arguments follow – (+ 3 2) adds 3 and 2 A lot of parentheses These define lists and also programs In the original LISP there were two fundamental data types: atoms and lists. A list was a finite ordered sequence of elements, where each element is in itself either an atom or a list, and an atom was a number or a symbol. For example, the list (FOO (BAR 1) 2) contains three elements: the symbol FOO, the list (BAR 1), and the number 2. (a b c d) is a list of 4 elements (atoms) a,b,c,d Case insensitive: aBc and ABC are the same atom

6 Relational Operators The condition expression (= N 1) is a relational expression. It returns boolean values T or NIL. In fact, LISP treats NIL as false, and everything else as true. 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

7 Some examples.. (+ (length "Hello World") 44)  55 (* (+ 3 2.3) (/ 3 (- 9 4)))  3.180 (log (log (log 234231232234234123)))  1.3052895 (+ (* (sin 0.3) (sin 0.3)) (* (cos 0.3) (cos 0.3)))  1 (and (< 3 (* 2 5)) (not (>= 2 6)))  T [ (3 = 6) ] (+ (print (* 2 3)) (print (/ 3 2)) 9)  6 3/2 33/2

8 Lisp Built-in Functions CAR Takes the first element of list (CAR `(ONE TWO)) Answer ONE CDR Remove the first element of list (CDR `(ONE TWO THREE)) Answer TWO THREE SETF Assigns values: (SETF NUMBER `32) Associates a list with a list name: (SETF COLORS `(YELLOW RED GREEN))

9 QUOTE Prints list item(s) (QUOTE `(COLORS)) Answer YELLOW RED GREEN EVAL Evaluates expression: (EVAL NUMBER) 23 APPEND Merges lists (APPEND `((A B)(C D))) Answer (A B C D)

10 LIST Makes a new list of several lists (LIST `((A B)(C D))) Answer ((A B)(C D)) CONS Adds a new first element to a list (CONS `A (Q Z)) Answer (A Q Z) ATOM Tests an argument to see if it is an atom (ATOM `(A B C D)) Answer NIL (ATOM `A) Answer T LISTP Tests an argument to see if it is a list (LISP `(A B C D)) Answer T (LISTP `A) Answer NIL

11 NULL Tests to see if an argument is an empty list (NULL '()) Answer T (NULL '(A B C)) Answer NIL MEMBERP Tests to see if an ATOM is a member of a list. NIL Universal false. Boolean response for "false" T Universal true. Boolean response for "true"

12 DEFUN Creates user defined functions Example: (DEFUN ADD2 (x y) (+ x y)) Creates a function called ADD2 that takes two parameters x and y and adds them. To use: (ADD2 3 4) Answer 7 ABS - Absolute Value (ABS -5) 5 EXPT - Calculates Powers (EXPT 3 2) 9 SQRT - Square Root (SQRT 16) 4.0 MAX - Largest number in sequence (MAX 2 5 6) 5 MIN - Smallest number in sequence (MIN 2 5 6) 2

13 Examples.. (length ‘(a b c)) => 3 (atom ‘a) => T (atom ‘(a b c)) => NIL (listp ‘a) => NIL (listp ‘(a b c)) => T

14 Control Structures and Variables (if test-expression then-expression optional-else-expression) (if (<= 3 2) (* 3 9) (+ 4 2 3))  9 [if 3<=2 then return 3.9 else return 4+2+3] (if (> 2 3) 9)  NIL [if 2>3 then return 9 else return nil] (if (= 2 2) (if (> 3 2) 4 6) 9)  4 [if 2==2, then if 3>2, then return 4 else return 6 else return 9] (+ 4 (if (= 2 2) (* 9 2) 7))  22 [NOTE: the 'if' evaluates to 18!]

15 (setf variable-symbol expression) (setf x (* 3 2))  6 X  6 (setf y (+ x 3))  9 (* x y)  54 (setf sin 9)  9 (sin sin)  0.412118 (+ x (setf x 3) x)  12 [Just like in C++/Java: x + (x = 3) + x ] ; + will return 6+3+3

16 Iteration (let (( ) ( ) …) ) – Declares local variables. It is best to declare variables before using them. (dotimes ( ) ) (let ((sum 0)) (dotimes (i 10 sum) (setq sum (+ sum i))) ) => 45 (dotimes (x 4 "yo") (print "hello")) "hello" "yo"

17 (dolist ( ) (let ((sum 0)) (dolist (i ‘(1 2 3) sum) (setq sum (+ sum i)) ))  6

18 Writing Functions (defun function-name-symbol (param1 param2 param3...) expr1 expr2 expr3... ) (defun hypoteneuse (length width) (sqrt (+ (* length length) (* width width)))) HYPOTENEUSE (hypoteneuse 6 8)  10

19 Recursion (defun power (base exp) (if (eq exp 0) 1 (* base (power base (- exp 1))) )) (power 3 2) => 9

20 Lists and Symbols as Data (quote (2 3 5 7 11 13 17 19)) (2 3 5 7 11 13 17 19) Notice that we have quoted the list using the quote special form. This is necessary because, without the quote, LISP would interpret the expression (2 3 5 7 11 13 17 19) as a function call to a function with name "2" and arguments 3, 5,..., 19 '(2 3 5 7 11 13 17 19) (2 3 5 7 11 13 17 19) The quote symbol ' is nothing but a syntactic shorthand for (quote...).

21 '(how are you today ?) ; A list of symbols. (HOW ARE YOU TODAY ?) '(1 + 2 * x) ; A list of symbols and numbers. (1 + 2 * X) '(pair (2 3)) ; A list containing 'pair and '(2 3). (pair (2 3)) Notice that the list (pair (2 3)) has length 2 (recursive-list-length '(pair (2 3))) 2 (first '(pair (2 3))) [The old name of first is car ((Contents of Address Register) ).] PAIR (rest '(pair (2 3))) [The old name of rest is cdr.((Contents of Decrement Register))] ((2 3))

22 Lisp Style [BAD Lisp Style Symbols] my_symbol_name mySymbolName MySymbolName MY_SYMBOL_NAME [A GOOD Lisp Style Symbol] my-symbol-name Although Lisp symbols are case-insensitive, ALWAYS use lower-case. [There is a good reason for this. Keep in mind that Lisp is an interactive system: both you and the system are producing text on the screen. Lisp systems spit out symbols in UPPER-CASE. By sticking with lower-case yourself, you can distinguish between the text you typed and the text the Lisp system generated.] Do NOT use underscores in symbols. Use hyphens.

23 As Paul Graham says, "treat setf as if there were a tax on its use." Lisp is a functional language. Learn to use functional style. One way you can tell you're using functional style is if you have very few (or even no) local variables, and rarely if ever use a global variable. [HORRIBLE Lisp Style] (defun do-the-math (x y z) (setf w (+ x y)) (setf n (* z w)) (+ x n)) [BETTER Lisp Style -- functional style] (defun do-the-math (x y z) (+ x (* z (+ x y))))

24 The whole language always available There is no real distinction between read-time, compile-time, and runtime. You can compile or run code while reading, read or run code while compiling, and read or compile code at runtime.

25 "Hello World" Program in LISP ;;; HWorld.lsp ;;;.lsp file extension ;;; ===================================== ;;; ;;; === HELLO WORLD SIMULATION ======== ;;; ;;;===================================== ;;; ;;; This function simply returns the string Hello World that is in quotes. (DEFUN HELLO-WORLD () (PRINT (LIST 'HELLO 'WORLD)))

26 Comments A Lisp comment begins with a semi-colon. The Lisp reader will ignore the remainder of a line once the semicolon is encountered. Some examples follow: ; This is a comment. ;;; so is this. (defun one ( arg1 ; the first arg. arg2 ; the second arg. if I put a ) here is ; would be ignored. ) 1 )

27 The second conventional form of a comment is a "block comment". Block comments begin with a '#|' and end with a '|#'. #| This is a block comment |# #| NOTE: this function is broken I'll fit it later. (defun biffed_fcn ( arg1 ) (+ nil 1 ) ) |#

28 input/output You can input/output data to: standard input/output, string or file A number of functions supported by the Lisp: (read) ;; reads the input from the standard input (print ‘a) ;; prints to the standard output (scanf…) (printf…) (format …) for formatted input and output (open..) (close..) for opening and closing the files (load..) reads and executes the file

29 Garbage Collection Garbage: At a given point in the execution of a program P, a memory location m is garbage if no continued execution of P from this point can access location m. Garbage Collection: Detect garbage during program execution GC invoked when more memory is needed Decision made by run-time system, not program This is can be very convenient. Example: in building text- formatting program, ~40% of programmer time on memory management.

30 ELIZA ELIZA is a classic example of the use of Lisp in a sample artificial intelligence (AI) application. ELIZA is a program that "simulates" a patient's discussion with a psychiatrist. A sample dialogue with ELIZA follows (user input is in lower case, with ELIZA's responses in upper case):

31 MAY I HELP YOU? :i want you to help me WHAT WOULD IT MEAN TO YOU IF YOU GOT ME TO HELP YOU? :im afraid to tell you YOURE NOT REALLY TALKING ABOUT ME, ARE YOU? :what does that mean WHY DO YOU ASK? :are you asking me WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM ASKING YOU? :why do you keep answering with questions DOES THAT QUESTION INTEREST YOU? :i dont know DONT YOU REALLY KNOW? :no ARE YOU SAYING NO JUST TO BE NEGATIVE? :i think so DO YOU REALLY THINK SO? :not always CAN YOU THINK OF A SPECIFIC EXAMPLE? :oh shut up IM NOT SURE I UNDERSTAND YOU FULLY? :goodbye GOODBYE

32 ; The main function, ELIZA, gets a line of input and ;prints the appropriate response. The user's input is ;saved for the next time through the loop so we can ;check if the user is repeating himself. DEFINE (ELIZA (LAMBDA () (PROG (KEY IN ON) (CALL -936) ;clear the screen (GC NIL) ;turn off garbage collection message (PRINT) (PRINTLINE '(MAY I HELP YOU?)) LOOP ;Get input. If the new input equals the old, complain. ;Otherwise check if input should stop ELIZA. (SETQ IN (READLINE)) (COND ((EQUAL IN ON) (PRIN1) (PRINTLINE '(PLEASE DO NOT REPEAT YOURSELF)) (GO LOOP)) ((EQUAL IN '(GOODBYE)) (RETURN 'GOODBYE))) (SETQ KEY (KEYSEARCH IN)) ;get the keyword number (PRIN1) (PRINTLINE (REPLY (CONJUGATE (CDR KEY)) (CAR KEY))) ;print response (SETQ IN ON) (GO LOOP) ) )))

33 As A Result.. Successful language symbolic computation, experimental programming Specific language ideas Expression-oriented: functions and recursion Lists as basic data structures Programs as data, with universal function eval Idea of garbage collection.

34 LISP CYCLES

35 Links and Resources http://www.cs.sfu.ca/CC/310/pwfong/Lisp/1/tutorial1.html http://www.cs.cmu.edu/~dst/LispBook/index.html http://en.wikipedia.org/wiki/Lisp_(programming_language) http://www.mars.cs.unp.ac.za/lisp/ Introduction to LISP; Milos Hauskrecht


Download ppt "EZGİ GENÇ 07104405. History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally."

Similar presentations


Ads by Google