For Monday Read Chapter 3 Homework: –Lisp handout 2.

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
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.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Yu-Tzu Lin ( 林育慈 )
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
CSCI 2210: Programming in Lisp
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
For Friday No reading (other than handout) Homework: –Chapter 2, exercises 5 and 6 –Lisp handout 1.
PRACTICAL COMMON LISP Peter Seibel 1.
Control in LISP More on Predicates & Conditionals.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
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.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
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,
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
LISP A brief overview.
Using Lisp Lisp is a interactive system
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Abstraction and Repetition
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Programming Languages
Lisp.
The general format of case is the following: (case <key form>
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
LISP primitives on sequences
Presentation transcript:

For Monday Read Chapter 3 Homework: –Lisp handout 2

Atoms The simplest thing in Lisp is an atom Numbers are atoms Anything that would be a standard identifier in C++, etc., is also an atom. But alphanumeric atoms must be quoted when we refer to them (otherwise Lisp assume that they are variable names and tries to evaluate them by finding their contents--which may not exist)

Evaluating Atoms > 9 9 > > 43.2e > > abc *** - EVAL: variable ABC has no value > 'abc ABC > 'fred FRED > 'H2O H2O

Lists Lists are the crucial data structure in LISP They look like a list of atoms (or lists) enclosed in parentheses: (a b c 24 (a b)) The list above has 5 members, one of them a list with two members Like alphanumeric atoms, lists must be quoted when you type them in to the interpreter to keep Lisp from evaluating them

nil nil or () is the empty list It is the only item in Lisp which is both an atom and a list (at the same time) It is also Lisp’s value for FALSE (just like 0 is the value for FALSE in C and C++ Note that Lisp also has a value for true: it is t

More Examples > '(a b c) (A B C) > () NIL Note that Lisp is not case sensitive. clisp echoes everything in uppercase. Some Lisps use lower case.

Function Calls in Lisp (functionname parameter1 parameter2...) (+ 3 5) All functions return values

Taking Lists Apart (first ‘(lisp is fun)) lisp (rest ‘(lisp is fun)) (is fun) (first ‘((the dog) likes me)) (the dog)

Why the Quote? Quoting prevents Lisp from attempting to evaluate the list If we had (first (a b c d)) Lisp would try to find a function a and apply that function to b c d, then apply first to the result

The Old Way In the original lisp, first was spelled car and rest was spelled cdr Allowed short-cuts (cadr ‘(a b c)) ≡ (car (cdr ‘(abc)) ≡ (first (rest ‘(abc)) Not often needed, because Common Lisp provides second, third, etc. to tenth

SETF Lisp does use variables Values are given using setf (setf friends ‘(elf dwarf human)) (setf enemies ‘(orc kobold)) (setf pi 3.14) (setf ab-list ‘(a b) xy-list ‘(x y))

True and False Lisp has built-in true and false values nil, or the empty list, is the false value t is the true value (though any non-nil value will be interpreted as true) No other values can be assigned to these two atoms

Making Lists To add an item to the head of a list, use cons –(cons ‘a ‘(b c)) (a b c) To combine two lists, use append –(append ‘(a b c) ‘(d e f)) (a b c d e f) To make a list from a set of items, use list –(list ‘a ‘b ‘c) (a b c) –(list ‘(a b) ‘c) ((a b) c)

Tricky Stuff (append ‘(a b c) ‘( )) (list ‘(a b c) ‘( )) (cons ‘(a b c) ‘( ))

Shortening Lists Use nthcdr to trim multiple off the front of a list –(nthcdr 2 ‘(a b c d)) (c d) –(nthcdr 50 ‘(a b c d)) nil Use butlast to trim elements off the back of a list –(butlast ‘(a b c d)) (a b c) –(butlast ‘(a b c d) 3) (a) Use last to get a list containing just the last element of a list –(last ‘(a b c d)) (d)

Length The length primitive counts the number of top-level elements in a list –(length ‘(a b)) 2 –(length ‘((a b) (c d))) 2 –(length (append ‘(a b) ‘((a b) (c d)))) 4

Reverse The reverse primitive reverses the order of the top-level elements of a list –(reverse ‘(a b c)) (c b a) –(reverse ‘((a b) (c d))) ((c d) (a b))

Association Lists Provide hash-type capabilities Example: –(setf sarah ‘((height.54) (weight 4.4))) –(assoc ‘weight sarah) (weight 4.4) If multiple matches, only first is returned

Numbers (/ ) (/ 27 9) 3 (/ 22 7) 22/7 (float (/ 22 7)) (round (/ 22 7)) 3 1/7 (- 8) -8 (- -8) 8 (/ 2) 1/2

More Numbers (max 2 4 3) 4 (min ) 2 (expt 2 3) 8 (expt 3 2) 9 (expt ) (sqrt 9) 3 (abs 5) 5 (abs –5) 5

Defining Functions (defun both-ends (whole-list) (cons (first whole-list) (last whole-list))) (defun both-end-two-params (l m) (cons (first l) (last m)))

Let’s Try One Define palindromize, a function that takes a list as its argument and returns a “palindrome” twice as long as the original.

LET and LET* Allow you to bind values to locals (defun both-ends-with-let (whole-list) (let ((element (first whole-list)) (trailer (last whole-list))) (cons element trailer))) LET does binding “in parallel” LET* allows binding to occur in sequence– so variables can use previous variable in value computation

Equality equal: same expression eql: same symbol or number (of same type) eq: same symbol =: same number

Examples (equal (+ 2 2) 4) t (equal (+ 2 3) 3) nil (equal ‘(this is a list) (setf l ‘(this is a list))) t (eql 4 4.0) nil (eql 4 4) t (= 4 4.0) t

Member (setf sentence ‘(tell me about your mother please)) (member ‘mother sentence) (mother please) (setf pairs ‘((father son) (mother daughter))) (member ‘mother pairs) nil

Keyword Arguments (setf pairs ‘((maple shade) (apple fruit))) (member ‘(maple shade) pairs) nil; uses EQL by default We can change using a keyword (member ‘(maple shade) pairs :test #’equal) ((maple shade) (apple fruit))

Type Predicates atom –(atom ‘pi) t –(atom pi) t –(atom ‘(red black)) nil numberp –(numberp ‘pi) nil –(numberp pi) t symbolp –(symbolp ‘pi) t –(symbolp pi) nil listp –(listp ‘pi) nil –(listp pi) nil –(listp ‘(a list with pi in it)) t

Detecting Empty Lists Use NULL or ENDP (null ‘(this is not empty)) nil (endp ‘(this is not empty)) nil (null ()) t (endp ()) t (null ‘aSymbol) nil (endp ‘aSymbol) error

Number Predicates zerop plusp evenp > < may take multiple arguments

AND, OR, and NOT AND and OR work as expected –AND returns nil if any of its arguments return nil –OR return nil if all of its arguments return nil –Both are short-circuiting –AND returns value of last argument if non return nil –OR returns value of first non-nil argument NOT turn non-nil values into nil and nil into t

Selection (if ) (if (symbolp day-or-date) ‘day ‘date) (when ) (when (> temp high) (setf high temp) ‘new-record) (unless )

COND (cond ( …) ( …) … ( …)) (cond ((eq thing ‘circle) (* pi r r)) ((eq thing ‘sphere) (* 4 pi r r)))

COND cont. If no test matches, returns nil Often include a final test clause t (matching anything) (cond ((> p.75) ‘very-likely) ((> p.5) ‘likely) ((> p.25) ‘unlikely) (t ‘very-unlikely))

Recursion Standard method for doing “repetition” in functional languages is recursion (defun myexpt (m n) (if (zerop n) 1 (* m (myexpt m (- n 1)))))

Recursion Inefficiency (defun count-elements (l) (if (endp l) 0 (+ 1 (count-elements (rest l)))))

Tail Recursion If we don’t manipulate the result of a recursive call before passing it on, that is called tail recursion In a tail recursive function, you need not save any but the most recent call (because the result of the last call is the result of all calls) Good lisps (and prologs) optimize tail-recursive functions/predicates, making them just as efficient as a corresponding loop

Counting More Efficiently (defun count-elements-cleverly (lis) (count-elements-cleverly-aux lis 0)) (defun count-elements-cleverly-aux (lis result) (if (endp lis) result (count-elements-cleverly-aux (rest lis) (+ 1 result))))

Iteration Lisp has several constructs that support iteration. DOTIMES is the equivalent of a for loop. DOLIST iterates through a list. DO and LOOP are general purpose structures.

DOTIMES (defun dotimes-expt (m n) (let ((result 1)) (dotimes (count n result) (setf result (* m result)))))

DOLIST (defun count-outlyers (list-of-elements) (let ((result 0)) (dolist (element list-of-elements result) (when (or (> element boiling) (< element freezing)) (setf result (+ result 1)))))))

DO (defun do-expt (m n) (do ((result 1) (exponent n)) ((zerop exponent) result) (setf result (* m result)) (setf exponent (- exponent 1))))

LOOP LOOP is simply an infinite loop which can be exited using a return form. If no return statement is ever executed, the loop is truly infinite. Form is simply (loop )

Transforming Lists MAPCAR applies a function to each member of a list, producing a list of the results (mapcar #’oddp ‘(1 2 3)) (t nil t) Can use with your own functions. Do not have to be predicates

More List Processing All of the following take a predicate and a list and return a list based on the predicate Remove-if Remove-if-not Count-if Count-if-not

Explicitly Calling Functions funcall (funcall #’append ‘(a b) ‘(x y)) (funcall #’ ) (funcall #’first ‘(a b c)) apply (apply #’append ‘((a b) (x y))) (apply #’+ ( )) (apply #’first ‘((a b c)))

And More Common Lisp is a huge language The handouts are intended to encourage you to try out and explore the language. This is the end of lecture on Lisp, but I will answer questions at the beginning of each class.