Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks."— Presentation transcript:

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

2 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 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) (+ 100 5) –not strongly typed

4 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 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 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 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 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 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 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 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 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 2.718 (exponential) –(sqrt 25) which returns 5

13 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 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 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 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 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 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 19 Example Write a LISP function that will add 3 to its input > (defun addthree (x) addthree >(addthree 5) 8 (+ x 3))

20 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 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 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 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 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 25 if - then LISP provides an if-then like function: (if ) If cond is trueThen take this action

26 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 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 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 ‘(45 103 122 10 345)) Notice: there is no do body

29 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 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 31 do List Example Given a list of ages: –(setf ages ‘(3 4 17 23 6 27 9 31 45)) –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 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 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 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 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 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 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 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 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 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 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)))


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

Similar presentations


Ads by Google