320 likes | 674 Views
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
E N D
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 • 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
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
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
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
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
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.
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.
Dialects – Racket • One tool is the Dr. Racket IDE • Homework will be graded in Racket
Getting Started - Examples • Numeric Literals • Characters • Strings • Functions • Functions • Macros • Special Operators
Oops (the debugger) Clicking on the stop signs Opens the Backtrace
Defining Functions • Fundamental building block for Lisp programs • Most common type of Lisp test question! • Define a function that … Definitions Window Interactions Window
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
Lisp Lambda Functions • (lambda lambda-listbody) • Similar to lambda calculus expr.: • lambda x . x + 2
Fundamental Data Types • A list is a sequence of elements, where each element can be another list
Fundamental Data Types • Racket also provides • Vectors • Hash tables • I/O • Arrays
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.
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)
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)
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)))
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)
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.
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
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
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