1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
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.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
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.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
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)
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
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 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.
Yu-Tzu Lin ( 林育慈 )
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
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.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Getting Started with Lisp
Variables, Expressions, and IO
Lists in Lisp and Scheme
Lecture 07 More Repetition Richard Gesick.
LISP A brief overview.
Nondeterministic Evaluation
Using Lisp Lisp is a interactive system
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
PHP.
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Common Lisp II.
Programming Languages
Lisp.
The general format of case is the following: (case <key form>
LISP primitives on sequences
Presentation transcript:

1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks

2 LISP LISP stands for LISt Processing Its development began in the early 50’s making it one of the oldest computer languages (only FORTRAN is older) –Common LISP –LISP 1.5 –X-Lisp –...

3 Why LISP? Why AI researchers like Lisp: –write programs that can write programs - Lisp programs are Lisp data structures –rapid prototyping interactive short edit-compile-test cycle –simple uniform syntax for programs and data structures –same syntax for operators and procedure calls (prefix notation) ( ) –not strongly typed

4 LISPWorks LISPWorks is a Windows version of LISP that is available from the net –check the class web site for a pointer It has been loaded on the PCs in the Computer Science labs

5 LISP Operation LISP is an interpreted language - each statement is evaluated after it is typed instead of waiting for the entire program. READ-EVALUATE-PRINT Loop –You enter: >(+ 3 2) –LISP returns: 5

6 LISP Expression –example > (max (* (+ 6 3) (- 6 3)) 6 (* 2 1)) ? –things to note “reverse polish” notation expressions are bracketed first item in an expression list is interpreted as a function name; the remaining items are arguments arguments are evaluated before being passed to the function functions can be either built in or user defined

7 First Lisp Program A simple LISP program for calculating the average of 3 numbers x, y, and z (defun average (x y z) (/ (+ x y z) 3)) Create a list beginning with the keyword defun Name the function and its parameters Enter the function body To run this program > (average 2 3 4) 3.0

8 Simple LISP Functions Common LISP has over 700 built-in functions –Note: the ‘ operator means do not go inside the list EXAMPLES cons: puts a list together > (cons ‘a ‘(b c)) (a b c) car: returns the first element of a list > (car ‘(a b c)) a length: returns the number of elements in a list > (length ‘(a b)) 2

9 Simple LISP I/O read is a function without arguments - when read is called, it causes LISP to wait (without a prompt) for you to type in a single expression –(read) print is a function of one argument which causes that argument to be printed to the screen –(print ‘enter)

10 Symbols & Numbers A list contains function names, symbol names, and/or numbers A symbol name identifies an atom –it has a “value” - sometimes called a binding –it has “properties” - they can be assigned –they evaluate to their current binding

11 Numbers In spite of the myth, LISP is a powerful language for numeric computing Numbers can be represented in several formats and they always evaluate to themselves –fixnum (integer) –single and double floating point –bignum (large integer)

12 Working with Numbers Arithmetic operations are expressed in prefix notations (operation first) –addition (+), subtraction (-), division (/), and multiplication (*) –(* 3 7) Other operations include –(mod 22 6) which returns 4 –(exp 1) which returns (exponential) –(sqrt 25) which returns 5

13 Assigning Values There is no assignment statement in LISP of the form t = 1.2 The assignment function is called setq or setf >(setq ‘a ‘23) 23 >a 23 >(setq ‘b (+ a 2)) 25

14 Rule of Three There are three basic rules that LISP follows when evaluating expressions –Symbols are replaced by their current bindings –Lists are evaluated as function calls –Everything else evaluates to itself

15 Lists in LISP There are three built-in LISP functions for the construction of lists cons : creates a list out of two arguments (the 2nd argument must be a list) > (cons ‘(a b) ‘(b c)) ((a b) b c) list: creates a list out of all its arguments > (list ‘a ‘b ‘c) (a b c) append: combines all its arguments into one list > (append ‘(a b) ‘(b c) ‘d) (a b b c d)

16 Breaking down lists The two most common built-in LISP functions for taking a list apart are: car : returns the first element of a list > (car ‘(a b c)) a cdr : returns the list with the first element removed > (cdr ‘(a b c)) (b c)

17 Other LISP Access Functions nth: returns the nth element of a list (0 indexed) –(nth 0 ’(a b c))  A –(nth 1 ’(a b c))  B nthcdr: returns the nth tail of a list –(nthcdr 0 ’(a b c))  (A B C) –(nthcdr 1 ’(a b c))  (B C) –(nthcdr 2 ’(a b c))  (C) –(nthcdr 3 ’(a b c))  NIL (empty list)

18 Custom LISP Functions Writing a program in LISP To create a new user-defined function in LISP use the built-in LISP function, defun FORMAT –(defun ( ) ( ))

19 Example Write a LISP function that will add 3 to its input > (defun addthree (x) addthree >(addthree 5) 8 (+ x 3))

20 LAST Function Another Common LISP built-in function is LAST which returns the last element of a list –(last (a b c)) returns c PROBLEM: Write a LISP function that will return both ends of a list

21 LISP Predicates atom tests to determine if an element is an atom (not a list) listp determines if an element is a list >(atom a) >(atom ‘(a b))>(listp ‘(a b))

22 More LISP Predicates I member takes two arguments, the second of which must be a list It determines if the first argument appears in the second If it is, then member returns the part of the list in which the match occurs (not nil result) >(member ‘b ‘(a b c)) >(member ‘d ‘(a b c))

23 More LISP Predicates II equal determines if two elements look alike numberp tests to determine if the argument is a number null : tests for an empty list >(equal ‘(a b) ‘(a b)) >(numberp ‘(a b))

24 Conditional Expressions A general form of the if … then … else structure is provided by the LISP function cond FORMAT (cond ( ) ( )... ( )) LISP evaluates the conditions in order, if the first true condition is condition i, it will evaluate expression i.

25 if - then LISP provides an if-then like function: (if ) If cond is trueThen take this action

26 Iteration The general structure for a do is: (do ( ( var1 init1 step1) ( var2 init2 step2)... (varn initn stepn)) (end-test return-result) do-body) Initialize the variables Check exit condition Evaluate the do body Update the variables

27 do Example PROBLEM: given a list, add the numbers in the list and print an error message if a symbol is found in the list (defun check-add (L) (do ((TL L (cdr TL))) ((null TL) sum) (cond ((numberp (car TL)) (setf sum (+ sum (car TL)))) (t (setf sum ‘error) (setf TL nil))))) (setf sum 0) What does this do? ? ? >(check-add ‘(1 2 3)

28 do Example II PROBLEM: Search a list of numbers to find all the elements greater than 100 and save them in a new list (defun save-large (L) (do (TL L (cdr TL)) (result nil (cond ((> (car TL) 100) (cons (car TL) result)) (t result)))) ((null TL) (reverse result))))?? >(save-large ‘( )) Notice: there is no do body

29 Other do Structures do* - with a do, all the variables are updated at the same time while a do* updates the variables one at a time dotimes - simple iteration (dotimes ( ) ) var begins at 0 and is updated by 1 after each iteration until it equals the maximum value, then result is returned

30 do list dolist will cycle through all the elements of a list (dolist ( ) ) var will take on values from the list in order and return result when done

31 do List Example Given a list of ages: –(setf ages ‘( )) –Count all the adults if adults are anyone 21 or over Adult Test: (defun adultp (age) (>= age 21) (defun count-adult (ages) (setf nadult 0) (dolist (age ages nadult) (if (adultp age) (setf nadult (+ 1 nadult)))))

32 Loop Funtion Most general form of iteration –loop will iterate until it hits a return statement, so the end test may come at any time. (loop (if (return )))

33 Local Variables let allows you to define local variables. It evaluates the initial-value forms in parallel. Form: (let ((variable-1 initial-value-1)... (variable-n initial-value-n)) expression-1 expression-m) > (let ((x 1) (y 2)) (+ x y)) 3

34 LISP I/O There are two basic I/O functions in LISP  read  print File I/O requires a open file statement and the specification of file parameters

35 read read is a function without arguments - when read is called, it causes LISP to wait (without a prompt) for you to type in a single expression.  (read) How would you read data into a variable? (setf x (read))

36 print print is a function of one argument which causes that argument to appear on the screen  print also supplies a “newline” before the argument and a space after it (print ‘(show this)) add a prompt to read (print ‘(Enter N)) (setf N (read))

37 File I/O General format for file I/O: (with-open-file Will close the file when done ( ) Specifications include direction :direction :input

38 Example I Read from a file: (with-file-open Declare the file name (fi “myfile.lsp” Set the direction :direction :input) Read the data (setf val (read fi)))

39 Example II Write to a file: (with-file-open Declare the file name (fo “myfile.lsp” Set the direction :direction :output) Write the data (print val fo))

40 Example III Read data from a file into a list: (setf L Nil) (with-file-open (fi “ListData.lsp” :direction :input) Use dotimes (dotimes (i 10) Read a data item (setf val (read fi)) Update List (setf L (cons val L))))

41 Example IV Print a list to a file: (setf L Nil) (with-file-open (fo “ListData.lsp” :direction :output) Use dolist (dolist (q L) (print q fo)))