1 / 93

Lisp

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

alden
Download Presentation

Lisp

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. Lisp Fundamentals

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

  3. Why Lisp? • great programming environment • Used at IRCAM, Grame, CCRMA, CNMAT, MIT, etc. for music

  4. Why programming? • Composing and Analysis applications harbor their creator's biases • The less bias the more apt you are to get what YOU want.

  5. Why computers? • faster • more accurate • able to tackle large amounts of data NOTE: both paper and computer algorithms

  6. Lisp is: • (1) high level • (2) functional • (3) symbolic • (4) interpreted • (5) recursive

  7. Programming Credo • Divide and Conquer

  8. Programming Credo • Divide and Conquer

  9. Programming Credo • Divide and Conquer

  10. Programming Credo • Divide and Conquer

  11. Programming Credo • Divide and Conquer

  12. Lisp Credo • Simple is beautiful (kiss) • Small is best

  13. Above All Readability is @#$%^&* everything.

  14. Need a text for reference: A Gentle Guide to Common Lisp By David Touretzky Free online

  15. Need a reference volume: Common Lisp The Language By Guy Steele Free online

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

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

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

  19. How it works • input in the Listener window is evaluated—interpreted—by Lisp whenever users follow input with a carriage return

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

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

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

  23. Data • Numbers like 1 2 3 4 etc. • Lists (1 2 3 4 5) is a list of numbers

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

  25. 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 (').

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

  27. 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 (‘).

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

  29. A quiz. What will the following produce? • ? (1 2)

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

  31. What will the following produce? • ? ‘(first ‘(1 2 3))

  32. Yikes! • (FIRST \‘ (1 2 3))

  33. What will the following produce? • ? (second ‘(1 2 3))

  34. Yep! • 2

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

  36. How about? • (* 1 2) • Give it some thought.

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

  38. Nesting • Lisp operations can also be nested to produce more interesting results. • ? (first (rest '(1 2 3)))

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

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

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

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

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

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

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

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

  47. Other primitives to know • length • reverse • append • third to tenth • nth (careful) • nthcdr (careful) • random (careful)

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

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

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

More Related