1 / 30

Introduction to Lisp Programming ( DrRacket )

Introduction to Lisp Programming ( DrRacket ). Merrill McKee – TA merrillmck@yahoo.com Tom Cool – TA tomcool@knights.ucf.edu. Why learn Lisp?. As a computer scientist, learn to think functionally One of three major programming language paradigms: Functional, Procedural, Object-Oriented

anja
Download Presentation

Introduction to Lisp Programming ( DrRacket )

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 (DrRacket) Merrill McKee – TA merrillmck@yahoo.com Tom Cool – TA tomcool@knights.ucf.edu

  2. Why learn Lisp? • As a computer scientist, learn to think functionally • One of three major programming language paradigms: • Functional, Procedural, Object-Oriented • Lisp is the oldest and premier functional language • (Lisp has actually grown into a multi-paradigm language, and includes procedural facilities.) Many programmers learn to love Lisp Perhaps I like Lisp because of some quirk in the way my brain is wired.” -- Peter Seibel

  3. Why learn Lisp? • 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 for your COP 4020 exams and homework References on last slide

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

  5. Dialects - Common Lisp • In the 1980s, the Common Lisp language specification attempted to standardize existing Lisp variants • Later standardized by ANSI X3.226-1994 • Now supports object oriented, imperative, and functional programming

  6. Common Lisp - Resources • “Practical Common Lisp” by Peter Seibel is available in electronic version at UCF Library “One Search” • “Common LISP, A Gentle Introduction to Symbolic Computation,” by David Touretzky is recommended

  7. Dialects – Scheme • Developed in late 1970s at MIT by Steele and Sussman • Minimalist • Textbook uses Scheme • "Although we write our program interpretation and transformation systems in Scheme, any language that supports both first-class procedures and assignment (ML, Common Lisp, Python, Ruby etc.) is adequate for working the exercises." • Daniel P. Friedman and Mitchell Want, Essentials of Programming Languages, [Cambridge, Mass: The MIT Press]: xix.

  8. Dialects – Racket • Developed in late 1990s by Matthias Felleisen • Racket can be considered: • a programming language—a dialect of Lisp and a descendant of Scheme; • a family of programming languages—variants of Racket, and more; or • a set of tools—for using a family of programming languages.

  9. Dialects – Racket • One tool is the Dr. Racket IDE • Homework will be graded in Racket

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

  11. Numeric Literals

  12. Oops (the debugger) Clicking on the stop signs Opens the Backtrace

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

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

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

  16. Fundamental Data Types • A list is a sequence of elements, where each element can be another list

  17. Fundamental Data Types • Racket also provides • Vectors • Hash tables • I/O • Arrays

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

  19. Eval Function

  20. 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) or maybe (eql 1 1.0) (equal 1 1.0) (equalp 1 1.0)

  21. Answers to Sample Problems Only two evaluate correctly: > (if(< 2 3)(print "Yes")(print "No")) "Yes“ > (+) 0 The rest throw exceptions due to syntax errors, typically references to an identifier before its definition (e.g., nil, eq, eql are all undefined)

  22. Binding Names to Functions • Define is used to bind a name to a value or a lambda expression. • Format (define function_name (lambda (parameters) <expression(s)>) Example: (define square_num (lambda (n) (* n n)))

  23. Binding Names to Functions (cont)

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

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

  26. If (Cond) Cond is a macro to handle multiple nested if statements. > (cond [(positive? -5) (error "doesn't get here")] [(zero? -5) (error "doesn't get here, either")] [(positive? 5) 'here]) 'here

  27. If

  28. Sample problem • Is x ∈ 2n, where n is all positive integers? • Functional solution: • Recursively subtract 2 from x. • If x = 0, x ∈ 2n, else x ∉ 2n

  29. Solution to sample problem

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