lisp fundamentals. lisp a malformed acronym for list processing designed to create computer programs...

93
Lisp Fundamentals

Upload: grant-hicks

Post on 17-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Lisp

Fundamentals

Page 2: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Lisp

• a malformed acronym for List Processing

• designed to create computer programs

• first implemented in the late 1950s by John McCarthy and his associates at MIT

• has many devotees, especially in the field of artificial intelligence

Page 3: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Why Lisp?

• great programming environment

• Used at IRCAM, Grame, CCRMA, CNMAT, MIT, etc. for music

Page 4: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Why programming?

•Composing and Analysis applications harbor their creator's biases

•The less bias the more apt you are to get what YOU want.

Page 5: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Why computers?

• faster•more accurate•able to tackle large amounts of data

NOTE:both

paper and

computer algorithms

Page 6: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Lisp is:

• (1) high level

• (2) functional

• (3) symbolic

• (4) interpreted

• (5) recursive

Page 7: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Programming Credo

•Divide and Conquer

Page 8: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Programming Credo

•Divide and Conquer

Page 9: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Programming Credo

•Divide and Conquer

Page 10: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Programming Credo

•Divide and Conquer

Page 11: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Programming Credo

•Divide and Conquer

Page 12: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Lisp Credo

•Simple is beautiful (kiss)

•Small is best

Page 13: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Above All

Readability is @#$%^&* everything.

Page 14: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Need a text for reference:

A Gentle Guide to Common Lisp

By David Touretzky

Free online

Page 15: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Need a reference volume:

Common LispThe Language

By Guy Steele

Free online

Page 16: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Common Lisp

• Originally available in many flavors, the most used form of Lisp today is Common Lisp, available on every standard

computer platform and uniform in use through the standard aforementioned reference manual (Steele 1990).

Page 17: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Assignment #1.

• Find and download a Common Lisp application (Free ones available for all platforms). Good ones to try:

• For PCs: Franz Lisp’s Allegro CL Free Express Edition (http://www.franz.com/downloads.lhtml)

• For MACs: Clozure CL (http://trac.clozure.com/ccl)

Page 18: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Getting started

• Booting—initializing—Common Lisp typically produces a Listener window ready for command-line input.

• Typically this produces a prompt such as ?, or >, or :, or whatever, with a blinking cursor to the right, waiting for your input.

• You’re now ready to begin programming.

Page 19: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

How it works

• input in the Listener window is evaluated—interpreted—by Lisp whenever users follow input with a carriage return

Page 20: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Saving code

• Code may also be typed into a text window

• code typed into a text window will not be interpreted until users explicitly invoke it by loading or some other implementation-dependent process (see the Lisp version you use for the documentation)

Page 21: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Information types

• While many important types of information exist in Common Lisp, the two most important types are functions and data.

• Functions are operatives that create or alter data in useful ways.

• Data represent information upon which functions operate.

Page 22: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Functions

• In Lisp, functions are typically used individually or combined with other functions to produce more complex processes and output.

• User-defined functions can be included alongside built-in functions, with the results—as the axiom goes—often greater than the sum of their parts.

Page 23: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Data

• Numbers like 1 2 3 4 etc.

• Lists (1 2 3 4 5) is a list of numbers

Page 24: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Parentheses

• In order to keep complicated processes in Common Lisp clear, the language requires parentheses, in part to distinguish data and functions, as well as to clarify different functionally initiated actions.

• In other words, when entering functions or data into a Listener window, users must partition their boundaries and firing order with parentheses.

Page 25: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

The single quote

• all information entered into the Listener window will be evaluated by Lisp unless preceded by a single quote (').

• Lisp knows numbers but not lists of numbers.

• Lisp does not already know letters, words, etc. and these must be preceded by that single quote (').

Page 26: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Example

• The following code provides a simple example of these concepts, where the "?" represents a typical prompt provided by the form of Common Lisp used:

• ? (first '(1 2 3)) Produces 1

Page 27: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

A closer look:

• ? (first '(1 2 3))

• “First” is a function that Lisp knows (a primitive) that returns the first element of its argument (what follows)

• ‘(1 2 3) is a list of numbers that Lisp does not know and thus the single quote (‘).

Page 28: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Summary

• In effect, Common Lisp evaluates the function first with which it is familiar, and then applies that function to the data with which it is not familiar. Functions always appear to the left in a parenthetical representation with data to the right.

Page 29: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

A quiz. What will the following produce?

• ? (1 2)

Page 30: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Error!

• > Error: Car of (1 2) is not a function name or lambda-expression.

• > While executing: "Unknown"• > Type Command-. to abort.• See the Restarts… menu item for further choices.

Page 31: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

What will the following produce?

• ? ‘(first ‘(1 2 3))

Page 32: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Yikes!

• (FIRST \‘ (1 2 3))

Page 33: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

What will the following produce?

• ? (second ‘(1 2 3))

Page 34: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Yep!

• 2

Page 35: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

You are programming!

• It’s anal! Don’t expect the program to do anything but what it is designed to do.

Page 36: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

How about?

• (* 1 2)

• Give it some thought.

Page 37: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Yep!

• 2

• Neither the number 1 nor the number 2 requires single quotes here because Lisp recognizes individual numbers.

Page 38: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Nesting

• Lisp operations can also be nested to produce more interesting results.

• ? (first (rest '(1 2 3)))

Page 39: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Produces

• 2

• Lisp first evaluates the function rest (meaning all but the first), which returns the rest of its argument '(2 3), and then evaluates the function first which returns the first element of the result of the rest operation.

Page 40: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Another primitive

• Lisp provides another primitive—a built-in function that accomplishes very simple things—in this case called second, that produces the same result but using just one function call as in

• ? (second '(1 2 3)).• 2

Page 41: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Sublists

• Data can also be nested in sublists according to whatever plan the programmer feels will reveal the information best. e.g.,

• ((0 60 1000 1 127) (1000 62 1000 1 127) (2000 64 1000 1 127) (3000 65 1000 1 127) . . .

Page 42: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Lengths

• Lists can be of any length. This can lead to difficulties in reading or accessing the data.

• Lisp therefore provides a method of assigning symbols to represent data.

• By using the Common Lisp function setf allows lots of data to be placed in a single named container.

Page 43: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

setf

• (setf primes ‘(1 2 3 5 7 11 13 17 19 23))

• allows users to use the symbol primes instead of the numbers themselves.

• Therefore, once set, placing the symbol primes after the “?” returns the list of primes, since Lisp now understands primes.

Page 44: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

As in

• ? Primes

• > ‘(1 2 3 5 7 11 13 17 19 23))

• Now one can use functions to access primes in the same way they can access the actual list as in

• ? (second primes)

• 2

Page 45: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

More primitives

• Another Common Lisp primitive, cons, creates lists from—typically—an atom (number or letter) and a list as in

• ? (cons 1 '(2 3)) returns > (1 2 3)

Page 46: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

More than one argument

• cons requires two arguments, "constructing" (the word from which cons derives) a new list combined from its two arguments.

Page 47: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Other primitives to know

• length

• reverse

• append

• third to tenth

• nth (careful)

• nthcdr (careful)

• random (careful)

Page 48: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Some more

• + (can have any number of args)• - (can have any number of args)• / (can have any number of args)• * (can have any number of args)• sort (be careful)• exp• sqrt• sin, cosin, tan (etc.)

Page 49: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Predicates

• Some Lisp functions serve as test functions as in

• (listp 1)• which returns nil because its argument

is not a list• (atomp ‘(1))• which returns nil because its argument

is not an atom• The suffix “p” stands for predicate

Page 50: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Conditionals

• Some functions in Common Lisp—called conditionals—test data for certain attributes.

• For example, the function if tests its first argument and returns its second argument if it proves true, or its third argument if it proves false.

Page 51: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Example

• ? (if (numberp 1) t nil)• Returns• T• because “1” is a number and thus

the first choice (t) is the result• Note that Lisp is case insensitive.

Page 52: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Named

• This combination is often termed an if-then-else clause.

Page 53: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Quiz

• ? (if (numberp (second ‘(a 2 b d f)) ‘yes ‘no)

returns

• > yes

Page 54: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Quiz

• ? (cons 1 (rest ‘(1 2 3 4 5)))

returns

> (1 2 3 4 5)

Whew.

Page 55: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Now you think of some questions!

Page 56: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Remember

• All Lisp expressions must have matched parentheses; that is, every open paren must appropriately have a closed paren.

• Document all of your work by placing comments after a “;” or enclosing in “”, or placing between #| this is documentation |#

• Name variables and everything you do such that later on you can tell what they mean and does.

Page 57: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Remember

•Readability is @#$%^&* everything

Page 58: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Let’s try for a few combinations

• (defvar *my-numbers* ‘(1 2 3))

• (if (equal (first *my-numbers*) 2)

(third *my-numbers*)

(first *my-numbers*))

What’s the answer?

Page 59: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Answer

• 1

Page 60: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Another

• (append

(append *my-numbers* *my-numbers*)

(rest *my-numbers*))

Page 61: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Answer

• (1 2 3 1 2 3 2 3)

Page 62: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

A little harder

• (if *my-numbers* (last *my-numbers*)

())

Page 63: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Answer

• (3)

• N.B. The function “last” returns a list of the last number. Weird, but true.

Page 64: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Try this:

• (if (equal *my-numbers* (1 2 3))

(reverse *my-numbers*))

Page 65: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Answer

• Error! (forget the single quote on data!!!)

Page 66: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Whew!

• (third ‘((((a)) b)(c ((d) e) f)(((g)) h I))

Page 67: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Answer

• (((g)) h I))

Page 68: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• One flew over the parentheses nest!

Page 69: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Hummmmmmm

• (+ (first *my-numbers*) (third *my-numbers*) (first *my-numbers*))

Note how much easier the above is to read than:

• (+ (first *my-numbers*)(third *my-numbers*)(first *my-numbers*))

• 4

Page 70: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

More exercises

• (nth 5 ‘(1 2 3 4 5 6))• 6• Remember to check whether the

primitive you’re using is zero or one based as in

• (nthcdr 5 ‘(1 2 3 4 5 6))• 6• whew

Page 71: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

More?

• (position 1 '(2 1 3 4 5))• 1

(position 2 '(2 1 3 4 5))0

And so on.

Page 72: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From (0 60 1000 1 64) get:

• 1) 0

• 2) 64

• 3) 1000

• 4) (1000 1 64)

• 5) (60 1000)

• 6) (0 60 1000)

• 7) (0 1)

• 8) ((0))

• 9) (60 1000 64)

• 10) (60 0)

Page 73: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From (((a) b) c) get:

• 1) a

• 2) c

• 3) b

• 4) (a b)

• 5) (a (b c)))

Page 74: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From (a b c d e) get:

• 1) (d e)

• 2) 5

• 3) (a (b (c (d (e)))))

• 4) (e d c b a)

• 5) (a b c d)

Page 75: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From a get:

• 1) (a)

• 2) (a . a)

• 3) (a (a))

• 4) ((a) a)

• 5) (a ())

Page 76: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From a and (b c) get:

• 1) (a b c)

• 2) ((a) b c)

• 3) (a (b c))

• 4) (a c)

• 5) (c b a)

Page 77: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• From (a b) and (c d) get:

• 1) (a b c d)

• 2) ((a b) c d)

• 3) ((a b)(c d))

• 4) (b a d c)

• 5) (a (b c) d)

Page 78: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (append '((1)) '(2 3))

Page 79: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• ((1) 2 3)

Page 80: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (append '((2)(3)) '(4 5))

Page 81: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• ((2) (3) 4 5)

Page 82: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (first '(((a)) (b c d e)))

Page 83: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• ((A))

Page 84: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (rest '(((((f))))))

Page 85: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• nil

Page 86: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (first '(rest (a b c)))

Page 87: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• rest

Page 88: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (first (second '(((1 2))(2 3 4)((5 6 7)))))

Page 89: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• 2

Page 90: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• (second (first '(((1 2))(2 3 4)((5 6 7)))))

Page 91: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

• nil

Page 92: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Portable Lisp for practice.

• From now on bring your laptops (with Lisp loaded) to class each time so as we do these you can check them yourself!!!!!

Page 93: Lisp Fundamentals. Lisp a malformed acronym for List Processing designed to create computer programs first implemented in the late 1950s by John McCarthy

Assignment

• Aside from bringing your computer and finding and practicing (reading about) lisp, etc. on the website there is a list of questions you will need to answer and turn in next week.

• We will be getting to music next week, so don’t get nonplussed yet.