950 likes | 1.19k Views
Lisp. Fundamentals. Lisp. a malformed acronym for Lis t P rocessing 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. Why Lisp?.
E N D
Lisp Fundamentals
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
Why Lisp? • great programming environment • Used at IRCAM, Grame, CCRMA, CNMAT, MIT, etc. for music
Why programming? • Composing and Analysis applications harbor their creator's biases • The less bias the more apt you are to get what YOU want.
Why computers? • faster • more accurate • able to tackle large amounts of data NOTE: both paper and computer algorithms
Lisp is: • (1) high level • (2) functional • (3) symbolic • (4) interpreted • (5) recursive
Programming Credo • Divide and Conquer
Programming Credo • Divide and Conquer
Programming Credo • Divide and Conquer
Programming Credo • Divide and Conquer
Programming Credo • Divide and Conquer
Lisp Credo • Simple is beautiful (kiss) • Small is best
Above All Readability is @#$%^&* everything.
Need a text for reference: A Gentle Guide to Common Lisp By David Touretzky Free online
Need a reference volume: Common Lisp The Language By Guy Steele Free online
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).
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)
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.
How it works • input in the Listener window is evaluated—interpreted—by Lisp whenever users follow input with a carriage return
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)
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.
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.
Data • Numbers like 1 2 3 4 etc. • Lists (1 2 3 4 5) is a list of numbers
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.
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 (').
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
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 (‘).
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.
A quiz. What will the following produce? • ? (1 2)
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.
What will the following produce? • ? ‘(first ‘(1 2 3))
Yikes! • (FIRST \‘ (1 2 3))
What will the following produce? • ? (second ‘(1 2 3))
Yep! • 2
You are programming! • It’s anal! Don’t expect the program to do anything but what it is designed to do.
How about? • (* 1 2) • Give it some thought.
Yep! • 2 • Neither the number 1 nor the number 2 requires single quotes here because Lisp recognizes individual numbers.
Nesting • Lisp operations can also be nested to produce more interesting results. • ? (first (rest '(1 2 3)))
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.
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
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) . . .
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.
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.
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
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)
More than one argument • cons requires two arguments, "constructing" (the word from which cons derives) a new list combined from its two arguments.
Other primitives to know • length • reverse • append • third to tenth • nth (careful) • nthcdr (careful) • random (careful)
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.)
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
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.