PRACTICAL COMMON LISP Peter Seibel 1.

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

1 Programming Languages and Paradigms Lisp Programming.
Computer Science 1620 Loops.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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?
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
PRACTICAL COMMON LISP Peter Seibel 1.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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
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.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
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.
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 (... ).....
PRACTICAL COMMON LISP Peter Seibel 1.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
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.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
PRACTICAL COMMON LISP Peter Seibel 1.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
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.
PRACTICAL COMMON LISP Peter Seibel 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CPS120 Introduction to Computer Science Iteration (Looping)
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Basic Introduction to Lisp
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
PRACTICAL COMMON LISP Peter Seibel 1.
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,
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Python Loops and Iteration
Modern Programming Languages Lecture 20 Fakhar Lodhi
Python: Control Structures
Getting Started with Lisp
LISP A brief overview.
Outline Altering flow of control Boolean expressions
First Lecture on Introductory Lisp
FP Foundations, Scheme In Text: Chapter 14.
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
CISC101 Reminders All assignments are now posted.
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Bindings, Scope, and Extent
Bindings, Scope, and Extent
Common Lisp II.
The general format of case is the following: (case <key form>
Presentation transcript:

PRACTICAL COMMON LISP Peter Seibel 1

CHAPTER 7 MACROS: STANDARD CONTROL CONSTRUCTS 2

MACROS: WHEN AND UNLESS Macros can be used to extend the standard library of functionality in Common Lisp. This chapter introduces some standard control-construct macros defined by Common Lisp. IF special operator provides the most basic form of conditional execution—if x, do y; otherwise do z (if condition then-form [else-form]) For example: (if (> 2 3) “Yes" "No") → "No" (if (> 2 3) “Yes") → NIL (if (> 3 2) “Yes" "No") → “Yes“ Here the then-form and else-form are each restricted to being a single Lisp form. 3

MACROS: WHEN AND UNLESS Another special operator, PROGN, executes any number of forms in order and returns the value of the last form. For instance, suppose in the middle of a spam-filtering program you wanted to both file a message as spam ( 垃圾郵件 ) and update the spam database when a message is spam. (if (spam-p current-message) (progn (file-in-spam-folder current-message) (update-spam-database current-message))) In this case, Common Lisp provides a standard macro WHEN: (when (spam-p current-message) (file-in-spam-folder current-message) (update-spam-database current-message)) 4

MACROS: WHEN AND UNLESS If it wasn't built into the standard library, WHEN can be defined as: (defmacro when (condition &rest body) `(if,condition A counterpart to the WHEN macro is UNLESS, which reverses the condition, evaluating its body forms only if the condition is false. (defmacro unless (condition &rest body) `(if (not,condition) 5

EXERCISES Define a recursive function build-list that will return a list of the first n integers (either ascending or descending). It takes one integer n as an argument and returns a list of the first n integers. > (build-list 5 '(a b c d e f g h)) (a b c d e) Write an iterative version of build-list. Call it ibuild-list. 6

COND Another case of IF expression: if a do x, else if b do y; else do z. (if a (do-x) (if b (do-y) (do-z))) Common Lisp provides a macro for expressing multibranch conditionals: COND. (cond (test-1 form*). (test-N form*)) 7 For example: (cond (a (do-x)) (b (do-y)) (t (do-z)))

AND, OR, AND NOT NOT is a function which takes a single argument and inverts its truth value, returning T if the argument is NIL and NIL otherwise. AND and OR are macros and they implement logical conjunction and disjunction of any number of subforms. AND stops and returns NIL as soon as one of its subforms evaluates to NIL. If all the subforms evaluate to non-NIL, it returns the value of the last subform. OR stops as soon as one of its subforms evaluates to non-NIL and returns the resulting value. If none of the subforms evaluate to true, OR returns NIL. For examples: (not nil) → T (not (= 1 1)) → NIL (and (= 1 2) (= 3 3)) → NIL (or (= 1 2) (= 3 3)) → T 8

LOOPING: DOLIST AND DOTIMES Lisp provides two macros, DOLIST and DOTIMES, for the common cases of looping over the elements of a list and counting loops. DOLIST loops across the items of a list, executing the loop body with a variable holding the successive items of the list. (dolist (var list-form [result-form]) body-form*) For instance: CL-USER> (dolist (x '(1 2 3)) (print x)) NIL If you want to break out of a DOLIST loop before the end of the list, you can use RETURN. CL-USER> (dolist (x '(1 2 3)) (print x) (if (evenp x) (return))) 1 2 NIL 9

LOOPING: DOLIST AND DOTIMES DOTIMES is the high-level looping construct for counting loops. (dotimes (var count-form [result-form]) body-form*) The count-form must evaluate to an integer. Each time through the loop var holds successive integers from 0 to one less than that number. For instance: CL-USER> (dotimes (i 4) (print i)) NIL Break 4 [5]> (dotimes (i 4) (format t "~a" i) ) 0123 NIL 10

LOOPING: DOLIST AND DOTIMES Because the body of both DOLIST and DOTIMES loops can contain any kind of expressions, we can also nest loops. For example, to print out the times tables from 1 × 1 = 1 to 20 × 20 = 400: (dotimes (x 20) (dotimes (y 20) (format t "~3d " (* (1+ x) (1+ y)))) (format t "~%")) 11

EXERCISES What are the meanings of the following functions? (defun it-intersection (x y) (let ((result-set nil)) (dolist (element x result-set) (when (member element y) (push element result-set))))) (defun it-fact (n) (let ((prod 1)) (dotimes (i n prod) (setf prod (* prod (+ i 1)))))) 12

EXERCISES What are the meanings of the following functions? (defun rec-ffo (x) "Recursive function." (cond ((null x) nil) ((oddp (first x)) (first x)) (t (rec-ffo (rest x))))) (defun it-ffo (list-of-numbers) "Iterative function." (dolist (e list-of-numbers) (if (oddp e) (return e)))) 13

DO DO loop lets us bind any number of variables and gives us complete control over how they change on each step through the loop. (do (variable-definition*) (end-test-form result-form*) statement*) Each variable-definition introduces a variable that will be in scope in the body of the loop. The full form of a single variable definition is a list containing three elements. (var init-form step-form) The init-form will be evaluated at the beginning of the loop and the resulting values bound to the variable var. Before each subsequent iteration of the loop, the step-form will be evaluated and the new value assigned to var. The step-form is optional; if it's left out, the variable will keep its value from iteration to iteration. 14

DO (do (variable-definition*) (end-test-form result-form*) statement*) At the beginning of each iteration, after all the loop variables have been given their new values, the end-test-form is evaluated. As long as it evaluates to NIL, the iteration proceeds, evaluating the statements in order. When the end-test-form evaluates to true, the result-forms are evaluated, and the value of the last result form is returned as the value of the DO expression. 15

DO For example: (do ((n 0 (1+ n)) (cur 0 next) (next 1 (+ cur next))) ((= 10 n) cur)) The step forms (1+ n), next, and (+ cur next) are all evaluated using the old values of n, cur, and next. Only after all the step forms have been evaluated are the variables given their new values. Mathematically inclined readers may notice that this is a particularly efficient way of computing the eleventh Fibonacci number. (do ((n 0 (1+ n)) (cur 0 next) (next 1 (+ cur next))) ((= 10 n) cur) (print cur)) (do (variable-definition*) (end-test-form result-form*) statement*) Note: > (1+ 2) 3

DO For example: (do ((i 0 (1+ i))) ((>= i 4)) (print i)) > (do ((i 0 (1+ i))) ((>= i 4))(print i)) NIL This loop is much more simply written using DOTIMES. (dotimes (i 4) (print i)) 17 (do (variable-definition*) (end-test-form result-form*) statement*)

DO The next loop demonstrates a DO loop that binds no variables. It loops while the current time is less than the value of a global variable, printing “Waiting” once a minute. Note that even with no loop variables, you still need the empty variables list. (do () ((> (get-universal-time) *some-future-date*)) (format t "Waiting~%") (sleep 60)) A test example: (do () ((> 0 100)) (format t "Waiting~%") (sleep 6) ) PS: Ctrl-C to exit. 18 (do (variable-definition*) (end-test-form result-form*) statement*)

DO Another example: (defun read-a-number () (do ((answer nil)) (nil) (format t "~&Please type a number: ") (setf answer (read)) (if (numberp answer) (return answer)) (format t "~&Sorry, ~S is not a number. Try again.” answer))) > (read-a-number) Please type a number: foo Sorry, FOO is not a number. Try again. Please type a number: (1 2 3) Sorry, (1 2 3) is not a number. Try again. Please type a number: (do (variable-definition*) (end-test-form result-form*) statement*)

THE MIGHTY LOOP The LOOP macro actually comes in two flavors—simple and extended. At least one of Common Lisp's original designers hated it. The simple LOOP: An infinite loop doesn't bind any variables. (loop body-form*) The forms in body are evaluated each time through the loop, which will iterate forever unless you use RETURN to break out. For example, you could write the previous DO loop with a simple LOOP. (loop (when (> (get-universal-time) *some-future-date*) (return)) (format t "Waiting...~%") (sleep 1)) 20

THE MIGHTY LOOP The extended LOOP It's distinguished by the use of certain loop keywords that implement a special-purpose language for expressing looping idioms. For instance, here's an DO loop that collects the numbers from 1 to 10 into a list: (do ((nums nil) (i 1 (1+ i))) ((> i 10) (nreverse nums)) (push i nums)) → ( ) The LOOP version: (loop for i from 1 to 10 collecting i) → ( ) 21

THE MIGHTY LOOP The following are some more examples of simple uses of LOOP. This sums the first ten squares: (loop for x from 1 to 10 summing (expt x 2)) → 385 This counts the number of vowels in a string: (loop for x across "the quick brown fox jumps over the lazy dog” counting (find x "aeiou")) → 11 This computes the eleventh Fibonacci number, similar to the DO loop used earlier: (loop for i below 10 and a = 0 then b and b = 1 then (+ b a) finally (return a)) → 55 The symbols across, and, below, collecting, counting, finally, for, from, summing, then, and to are some of the loop keywords whose presence identifies these as instances of the extended LOOP. 22

TIME MACRO FUNCTION The TIME macro function tells you how long it took to evaluate an expression. It may also tell you how much memory was used during the evaluation, and other useful things. The exact details of what TIME measures and how the information is displayed are implementation dependent. Here is an example: (defun addup (n) "Adds up the first N integers" (do ((i 0 (+ i 1)) (sum 0 (+ sum i))) ((> i n) sum))) > (time (addup )) Real time: sec. Run time: sec. Space: Bytes GC: 26, GC time: sec