For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.

Slides:



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

Functions Robin Burke CSC 358/
ANSI Common Lisp 5. Control 16 June Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body.
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)
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.
Higher Order Functions “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction,
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
COMMON LISP Symbolic Expressions The syntactic elements of the LISP programming language are symbolic expressions. –Known as s-expressions. –Both programs.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Project 1: Background Informed search : 8- puzzle world BFS, DFS and A* algorithms Heuristics : Manhattan distance, Number of misplaced tiles Lisp programming.
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.
CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in.
Common Lisp! John Paxton Montana State University Summer 2003.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
For Friday Finish chapter 3 Homework: –Chapter 3, exercise 6 –May be done in groups. –Clarification on part d: an “action” must be running the program.
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.
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
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 (... ).....
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.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Common lisp A functional programming language. Useful URL:
Common Lisp! John Paxton Montana State University Summer 2003.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.
LISP John McCarthy and Marvin Minsky formed MIT’s AI Project in Suggested reading: John McCarthy’s home page
CSCI 2210: Programming in Lisp
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
For Friday Finish reading chapter 4 Homework: –Lisp handout 4.
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.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
PRACTICAL COMMON LISP Peter Seibel 1.
For Friday Read chapter 4, sections 1 and 2 Homework –Chapter 3, exercise 7 –May be done in groups.
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.
Control Structures CSC 358/ Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration.
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
Functional Programming: Lisp MacLennan Chapter 10.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Modern Programming Languages Lecture 20 Fakhar Lodhi
Getting Started with Lisp
LISP A brief overview.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Programming Languages
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.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3

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.

Solving Problems Getting from the current state of the world to the state we want the world to be in. May or may not matter how we get there.

Problem Formulation Initial state Goal state Operators that change the state of the world Path cost – for when it matters how we get there

Toy Problems 8-puzzle N-queens Peg puzzle Farmer, wolf, goat and cabbage Missionaries and cannibals

More Realistic Problems Route finding Traveling Salesman Problem VLSI layout Robot navigation Web searching