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

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Messiaen Quartet for the end of time And another.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
1 Programming Languages and Paradigms Lisp Programming.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
TES3111 October 2001 Artificial Intelligence LISP.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Fundamentals of Python: From First Programs Through Data Structures
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Genetic Programming.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Fundamentals of Python: First Programs
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Introduction to Programming with RAPTOR
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming in Scheme and Lisp.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Machaut Messe Performance Performance Performance Isorhythm Isorhythm.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Welcome to the Digital Arts Research Center (DARC)
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Fundamentals of Programming I Overview of Programming
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
LISP LISt Processing.
LISP A brief overview.
First Lecture on Introductory Lisp
The Metacircular Evaluator
T. Jumana Abu Shmais – AOU - Riyadh
The Metacircular Evaluator (Continued)
LISP A brief overview.
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
Lists in Lisp and Scheme
Presentation transcript:

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 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 ( For MACs: Clozure CL ( )

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 etc. Lists ( ) 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., (( ) ( ) ( ) ( )...

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

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.

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

Quiz ? (if (numberp (second ‘(a 2 b d f)) ‘yes ‘no) returns > yes

Quiz ? (cons 1 (rest ‘( ))) returns > ( ) Whew.

Now you think of some questions!

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.

Remember Readability everything

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?

Answer 1

Another (append (append *my-numbers* *my-numbers*) (rest *my-numbers*))

Answer ( )

A little harder (if *my-numbers* (last *my-numbers*) ())

Answer (3) N.B. The function “last” returns a list of the last number. Weird, but true.

Try this: (if (equal *my-numbers* (1 2 3)) (reverse *my-numbers*))

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

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

Answer (((g)) h I))

One flew over the parentheses nest!

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

More exercises (nth 5 ‘( )) 6 Remember to check whether the primitive you’re using is zero or one based as in (nthcdr 5 ‘( )) 6 whew

More? (position 1 '( )) 1 (position 2 '( )) 0 And so on.

From ( ) get: 1) 0 2) 64 3) ) ( ) 5) ( ) 6) ( ) 7) (0 1) 8) ((0)) 9) ( ) 10) (60 0)

From (((a) b) c) get: 1) a 2) c 3) b 4) (a b) 5) (a (b c)))

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)

From a get: 1) (a) 2) (a. a) 3) (a (a)) 4) ((a) a) 5) (a ())

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)

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)

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

((1) 2 3)

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

((2) (3) 4 5)

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

((A))

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

nil

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

rest

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

2

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

nil

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

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.