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

Slides:



Advertisements
Similar presentations
Introduction to LISP Programming of Pathway Tools Queries and Updates.
Advertisements

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is the only older.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Lisp and Scheme I. Versions of LISP LISP is an acronym for LISt Processing language Lisp is an old language with many variants – Fortran is the only older.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming Languages
Functional Programming
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Getting Started with Lisp
LISP LISt Processing.
Functional Languages Early in AI research, there was a need for symbolic computing handling symbols instead of numbers or strings recursion Design goals.
LISP A brief overview.
First Lecture on Introductory Lisp
The Metacircular Evaluator
Modern Programming Languages Lecture 21 Fakhar Lodhi
CS 36 – Chapter 11 Functional programming Features Practice
Lisp and Scheme I.
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Presentation transcript:

EZGİ GENÇ

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

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.

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)

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

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

Some examples.. (+ (length "Hello World") 44)  55 (* ( ) (/ 3 (- 9 4)))  (log (log (log )))  (+ (* (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

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

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)

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

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"

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

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

Control Structures and Variables (if test-expression then-expression optional-else-expression) (if (<= 3 2) (* 3 9) ( ))  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!]

(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)  (+ x (setf x 3) x)  12 [Just like in C++/Java: x + (x = 3) + x ] ; + will return 6+3+3

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"

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

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

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

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

'(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))

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.

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

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.

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

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 )

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

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

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.

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

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

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

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.

LISP CYCLES

Links and Resources Introduction to LISP; Milos Hauskrecht