1 / 26

Introduction to Lisp Programming

Introduction to Lisp Programming. Merrill McKee – TA merrillmck@yahoo.com. Why learn about Lisp?. Lisp programmers love Lisp “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

luce
Download Presentation

Introduction to Lisp Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Lisp Programming Merrill McKee – TA merrillmck@yahoo.com

  2. Why learn about Lisp? • Lisp programmers love Lisp • “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) • NASA’s 1998 Deep Space 1 mission • Common in Artificial Intelligence • PhD qualifying exam • Or just for your grade on homework #2 and COP 4020 exams References on last slide

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

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

  5. Getting Started - Examples • Numeric Literals • Characters • Strings • Functions • Functions • Macros • Special Operators

  6. Numeric Literals

  7. Oops (the debugger)

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

  9. History of Lisp • Created by John McCarthy in the late 1950’s • 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

  10. Lambda Calculus • 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

  11. Lisp Lambda Functions • (lambda lambda-listbody) • Similar to lambda calculus expr.: • lambda x . x + 2

  12. Fundamental Data Types • Two fundamental data types • Atoms • An atom is an alphanumeric string, used either as a variable name or a data item • Lists • 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

  13. The Interpreter • In 1965 McCarthy 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.

  14. Eval Function

  15. Sample Problems • 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) … (equal 1 1.0) … (equalp 1 1.0)

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

  17. Binding Names to Functions (cont) • Once the function is evaluated by the interpreter, it can be used as in (square 7) = 49 • Example (define (hypotenuse side1 side2) (sqrt (+ (square side1) (square side2))))

  18. Binding Names to Values • (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.

  19. If • John McCarthy invented the if-then-else 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.

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

  21. If

  22. If

  23. Setf • Binds a value to a location/variable • (setf place value)

  24. Sample problem • How would you reverse a list? Do you have enough tools yet to define this function? • 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

  25. My-reverse • http://mwolson.org/notes/wikisource/CommonLispBeginnersGuide.muse

  26. References For These Slides • UCF Library – about 20-25 books in the QA 76.73 .L23 … shelf. • 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/node1.html extensive but harder to read • http://www.google.com

More Related