introduction to lisp programming ( drracket )

30
Introduction to Lisp Programming (DrRacket) Merrill McKee – TA [email protected] Tom Cool – TA [email protected]

Upload: anja

Post on 22-Feb-2016

104 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Lisp Programming ( DrRacket ). Merrill McKee – TA [email protected] Tom Cool – TA [email protected]. Why learn Lisp?. As a computer scientist, learn to think functionally One of three major programming language paradigms: Functional, Procedural, Object-Oriented - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Lisp  Programming ( DrRacket )

Introduction to Lisp Programming (DrRacket)

Merrill McKee – [email protected] Cool – [email protected]

Page 2: Introduction to Lisp  Programming ( DrRacket )

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

Page 3: Introduction to Lisp  Programming ( DrRacket )

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

Page 4: Introduction to Lisp  Programming ( DrRacket )

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

Page 5: Introduction to Lisp  Programming ( DrRacket )

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

Page 6: Introduction to Lisp  Programming ( DrRacket )

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

Page 7: Introduction to Lisp  Programming ( DrRacket )

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.

Page 8: Introduction to Lisp  Programming ( DrRacket )

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.

Page 9: Introduction to Lisp  Programming ( DrRacket )

Dialects – Racket

One tool is the Dr. Racket IDE Homework will be graded in Racket

Page 10: Introduction to Lisp  Programming ( DrRacket )

Getting Started - Examples

Numeric Literals Characters

Strings Functions

Functions Macros Special

Operators

Page 11: Introduction to Lisp  Programming ( DrRacket )

Numeric Literals

Page 12: Introduction to Lisp  Programming ( DrRacket )

Oops (the debugger)

Clicking on the stop signsOpens the Backtrace

Page 13: Introduction to Lisp  Programming ( DrRacket )

Defining Functions Fundamental building

block for Lisp programs

Most common type of Lisp test question! Define a function

that …

Definitions Window

Interactions Window

Page 14: Introduction to Lisp  Programming ( DrRacket )

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

Page 15: Introduction to Lisp  Programming ( DrRacket )

Lisp Lambda Functions

(lambda lambda-list body)

Similar to lambda calculus expr.: lambda x . x + 2

Page 16: Introduction to Lisp  Programming ( DrRacket )

Fundamental Data Types

A list is a sequence of elements, where each element can be another list

Page 17: Introduction to Lisp  Programming ( DrRacket )

Fundamental Data Types

Racket also provides Vectors Hash tables I/O Arrays

Page 18: Introduction to Lisp  Programming ( DrRacket )

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.

Page 19: Introduction to Lisp  Programming ( DrRacket )

Eval Function

Page 20: Introduction to Lisp  Programming ( DrRacket )

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)

Page 21: Introduction to Lisp  Programming ( DrRacket )

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)

Page 22: Introduction to Lisp  Programming ( DrRacket )

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

Page 23: Introduction to Lisp  Programming ( DrRacket )

Binding Names to Functions (cont)

Page 24: Introduction to Lisp  Programming ( DrRacket )

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)

Page 25: Introduction to Lisp  Programming ( DrRacket )

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.

Page 26: Introduction to Lisp  Programming ( DrRacket )

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

Page 27: Introduction to Lisp  Programming ( DrRacket )

If

Page 28: Introduction to Lisp  Programming ( DrRacket )

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∈ ∉

Page 29: Introduction to Lisp  Programming ( DrRacket )

Solution to sample problem

Page 30: Introduction to Lisp  Programming ( DrRacket )

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