Introduction to Lisp Programming Merrill Mc Kee TA

  • Slides: 26
Download presentation
Introduction to Lisp Programming Merrill Mc. Kee – TA merrillmck@yahoo. com

Introduction to Lisp Programming Merrill Mc. Kee – TA merrillmck@yahoo. com

Why learn about Lisp? n Lisp programmers love Lisp ¡ n n n “Perhaps

Why learn about Lisp? n Lisp programmers love Lisp ¡ n n n “Perhaps I like Lisp because of some quirk in the way my brain is wired. ” Peter Seibel Still used in industry As extendable and as fast [1] as other languages Stable, powerful ¡ ¡ Web, GUI, open source libraries Read-eval-print loop (REPL) n n NASA’s 1998 Deep Space 1 mission Common in Artificial Intelligence Ph. D qualifying exam Or just for your grade on homework #2 and COP 4020 exams References on last slide

Common Lisp n Common Lisp, a dialect of Lisp, is the language specification that

Common Lisp n Common Lisp, a dialect of Lisp, is the language specification that was attempted in the 1980’s to standardize existing Lisp variants ¡ n Scheme Later standardized by ANSI X 3. 226 -1994 n Your homework assignment will be done in Common Lisp

Getting Started (link for more info) n Countless Lisp development environments Cygwin or Unix/Linux

Getting Started (link for more info) n Countless Lisp development environments Cygwin or Unix/Linux prompt Emacs Lispworks Eclipse w/ plugin

Getting Started - Examples n n Numeric Literals Characters ¡ n Strings Functions ¡

Getting Started - Examples n n Numeric Literals Characters ¡ n Strings Functions ¡ ¡ ¡ Functions Macros Special Operators

Numeric Literals

Numeric Literals

Oops (the debugger)

Oops (the debugger)

Defining Functions n n Fundamental building block for Lisp programs Most common type of

Defining Functions n n Fundamental building block for Lisp programs Most common type of Lisp test question! ¡ Define a function that …

History of Lisp n Created by John Mc. Carthy in the late 1950’s ¡

History of Lisp n Created by John Mc. Carthy in the late 1950’s ¡ ¡ n Published in 1960 Championed mathematical logic to study artificial intelligence Main idea was to study computability from a functional programming standpoint ¡ Common Lisp now supports object oriented, imperative, and functional programming

Lambda Calculus n n n Invented by Church and Kleene in the 1930’s Can

Lambda Calculus n n n Invented by Church and Kleene in the 1930’s Can be used to define what a computable function is Influenced functional programming languages such as Lisp, ML, and Haskell f(x) = x + 2 …or… lambda x. x + 2 Binds functions to names ¡ Gives a natural representation for recursion

Lisp Lambda Functions n n (lambdalist body) Similar to lambda calculus expr. : ¡

Lisp Lambda Functions n n (lambdalist body) Similar to lambda calculus expr. : ¡ lambda x. x + 2

Fundamental Data Types n Two fundamental data types ¡ Atoms n ¡ Lists n

Fundamental Data Types n Two fundamental data types ¡ Atoms n ¡ Lists n n An atom is an alphanumeric string, used either as a variable name or a data item A list is a sequence of elements, where each element is either an atom or a list Common Lisp also provides ¡ ¡ Vectors Hash tables I/O Arrays and Multi-dimensional arrays

The Interpreter n n n In 1965 Mc. Carthy developed a function called “eval”

The Interpreter n n n In 1965 Mc. Carthy developed a function called “eval” used to evaluate other functions (an interpreter). It is a read-evaluate-write infinite loop. Expressions are interpreted by the function “eval. ” Literals are evaluated to themselves. For example, if you type in 5, the interpreter will return 5. Expressions that are calls to primitive functions are evaluated as follows: ¡ ¡ ¡ Each parameter is evaluated first. The primitive function is applied to the parameter values. The result is displayed.

Eval Function

Eval Function

Sample Problems n What do the following evaluate to? (Use your intuition and guess.

Sample Problems n What do the following evaluate to? (Use your intuition and guess. No grade here. In the spirit of the class, please come up with an answer before using your laptop to find a solution. ) ¡ ¡ ¡ (+ (3 2)) (if (< 2 3) (print “Yes”) (print “No”)) (if (< 2 3) (print “ 1”) (print “ 2”) (print “ 3”)) z (x 1 “foo”) … or … ‘(x 1 “foo”) (+ 1) (dotimes (x 2) (print x)) (null nil) () (sort ‘(1 2 3) #’>) (eq 1 1. 0) …. . (eql 1 1. 0) … (equalp 1 1. 0)

Binding Names to Functions n Define or defun* is used to bind a name

Binding Names to Functions n Define or defun* is used to bind a name to a value or a lambda expression. ¡ n *Depends on the Lisp environment Format (defun (function_name parameters) <expression(s)>) Formally - defun name lambda-list [[ {declaration}* | doc-string ]] { form }* n Example (defun (square num) (* num))

Binding Names to Functions (cont) n n Once the function is evaluated by the

Binding Names to Functions (cont) n n Once the function is evaluated by the interpreter, it can be used as in (square 7) = 49 Example (define (hypotenuse side 1 side 2) (sqrt (+ (square side 1) (square side 2))))

Binding Names to Values n n n (define pi 3. 14) (define twopi (*

Binding Names to Values n n n (define pi 3. 14) (define twopi (* 2 pi)) Once these two expressions are typed in to the Lisp interpreter, typing pi will return 3. 14. Names consist of letters, digits, and special characters (except parenthesis) but cannot begin with a digit.

If n n John Mc. Carthy invented the if-thenelse construct we take for granted.

If n n John Mc. Carthy invented the if-thenelse construct we take for granted. It was incorporated into Algol. (if condition then-form [else-form]) ¡ ¡ The then-form and optional else-form are restricted to a single lisp form. To create a “code block” use the progn function. Alternatively, use when and unless macros.

If (Cond) n n Cond is a macro to handle multiple nested if statements.

If (Cond) n n Cond is a macro to handle multiple nested if statements. (cond (test-1 form*) … (test-n form*))

If

If

If

If

Setf n n Binds a value to a location/variable (setf place value)

Setf n n Binds a value to a location/variable (setf place value)

Sample problem n How would you reverse a list? Do you have enough tools

Sample problem n How would you reverse a list? Do you have enough tools yet to define this function? ¡ ¡ n Without using the built-in reverse function (defun reverse-list (list) … ) What do you need? ¡ ¡ ¡ Need to get an element from the list Need to build and save a new list Need to do it in reverse order

My-reverse nhttp: //mwolson. org/notes/wikisource/Common. Lisp. Beginners. Guide. muse

My-reverse nhttp: //mwolson. org/notes/wikisource/Common. Lisp. Beginners. Guide. muse

References For These Slides n UCF Library – about 20 -25 books in the

References For These Slides n UCF Library – about 20 -25 books in the QA 76. 73. L 23 … shelf. ¡ ¡ n n Practical Common Lisp by Peter Seibel Common Lisp The Language by Guy Steele, Jr. Dr. Montagne’s UCF COP 4020 Slides Websites ¡ ¡ ¡ http: //mypage. iu. edu/~colallen/lp/ concise and readable http: //www. cs. cmu. edu/Groups/AI/html/cltl/clm/n ode 1. html extensive but harder to read http: //www. google. com