ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.

Slides:



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

Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
ITC 240: Web Application Programming
Computer Science 1620 Loops.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
>(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?
Chapter 5: Loops and Files.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Mapping And Iteration So far, the only Lisp mechanism we have considered, which allows an action to be performed repeatedly is recursion. Most programming.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
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.
David Stotts Computer Science Department UNC Chapel Hill.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Control Structures CSC 358/ Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Repetition Structures
Introduction To Repetition The for loop
Getting Started with Lisp
While Loops in Python.
Arrays & Functions Lesson xx
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Arrays, For loop While loop Do while loop
Lesson #5 Repetition and Loops.
Java Programming Loops
Repetition Structures
Introduction to Problem Solving and Control Statements
Modern Programming Languages Lecture 20 Fakhar Lodhi
Loop Construct.
Java Programming Loops
Abstraction and Repetition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Conditional Loops Counted Loops
Common Lisp II.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
While Loops in Python.
The general format of case is the following: (case <key form>
Presentation transcript:

ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens on each iteration of the loop The variable declaration creates a variable (sort of like a counter variable in a for loop) and specifies a list of values that the variable will be given (one value for each iteration of the loop). The variable declaration also specifies the value that will be returned when the dolist function ends. (dolist (var ‘(value1 value2 value3) ‘return-value ) (do-something var) )

DOLIST EXAMPLE A different way of having a particular computation applied to all the elements of a list: > (dolist (x ‘( ) ‘t) (cond ((evenp x) (print x))) ) t Here, X is the local variable, ( ) is the list to be scanned, ‘t is the result of the expression (returned when the dolist ends); there is only one statement in the body, to be executed for each value of X: there could be more!

Another DOLIST example Collecting up values in a DOLIST: suppose we want to add up all the even numbers in our list, so if the list is ‘( ) we’ll get =20 as our answer. Here’s a DOLIST for this: (let ((sum 0) ) (dolist (x ‘( ) sum) (cond ( (evenp x) (setf sum (+ sum x)) ) ) ) ) Notice that the value returned when this ends will be sum ! This example uses a number things we’ve seen before: let (to set up a storage variable), setf (to change the value of that variable), and the dolist.

DOTIMES DOTIMES allows the repetition of a computation a fixed number of times. As with DOLIST, its arguments are a variable declaration and a body. In DOTIMES, the variable is a counter whose value starts at 0 and which increments until it hits the specified stopping value. For each value of the counter, the body of the DOTIMES is executed. When the counter hits the stop-value, the specified return-value is returned as the value of the expression. (dotimes (counter stop-value return-value ) (do-something var) )

DOTIMES example > (dotimes (c 4 ‘done) (print (“Counter = “) (print c)) This piece of lisp will repeat the body (the two print statements) four times (c=0 to c=3). When the loop ends, the value ‘done will be returned. Counter = 0 Counter = 1 Counter = 2 Counter = 3 done

DOTIMES for factorial Recall that factorial N = N*(N-1)*(N-2)…1 We saw a recursive statement of factorial in our 2 nd lecture: (defun factorial(N) (if (= N 0) 1 (* N (factorial (- N 1))) )) Here’s an iterative version of factorial: (defun iterativeFactorial(N) (let ( (result 1) ) (dotimes (count (+ N 1) result) (setf result (* result count)) ) ) )

LOOP The loop command takes no arguments, apart from the sequence of expressions which form the body of the iteration: (loop (let ((value (read))) (cond ((null value) (return)) (t (print value)) ) Body of the loop (read) is a built-in function that reads a value from the user. This piece of code will simply echo whatever the user types in.

RETURN The return statement is for exiting from any iteration other than at the natural termination of the looping or passing results back from an iteration We can use return in DOLIST or DOTIMES if we want, or in any other iterative construct. In the LOOP expression, we have to use RETURN to end the loop (LOOP has no predefined check statement for loop finish).

DO iterative construct DO is more complicated than the the other iterative constructs DOLIST and DOTIMES IN a DO statement we can have a list of variables, each of which takes on a different value on each iteration of the loop. Each variable declaration in a DO consists of a list: ( variable-name initial-value computed-new-value ) Overall a DO expression has three parts: –A list of variable declarations as above (a list of lists) –A (finished-yet? return-value) pair, to check if the loop is over yet –A body (although in many DO expressions, no body is needed since most of the work is done in computing the new values for variables)

DO (defun average (L) (do ( (sum 0.0 (+ sum (first values))) (count 0 (+ count 1)) (values L (rest values)) ) ; initial values and updates for variables in do-loop ( (null values) (/ sum count) ) ; finished? test and result returned ); end of do-loop (no body) ); end of function In this function, the variable values starts out holding the input list L. On each iteration of the loop values becomes (rest values). The loop continues until (null values) is true. On each cycle of the loop, the number in (first values) is added to the sum variable, and the count variable goes up by one. When the loop finishes, we return (/ sum count) as the average of the numbers in the list.

Iteration vs. Recursion Scanning all the elements in a list: MAPC (if no results to be returned). MAPCAR (if results to be returned); or DOLIST Repeating a computation a number of times: DOTIMES, DO Repeating until some condition is met: DO and LOOP Testing whether some condition is met for elements in the list. ?

EXERCISE How would you write the function REMOVE-ITERATIVE, given a data-item X and a list L as arguments, that returns a list which is the same as L except that any items equal to X do not appear in this new list using DO rather than recursion? This will involve carefully coding how the variables in the DO expression get their new values on each iteration of the loop. Two variables: –values : holding the input list initially, and going down that list one element at a time using rest, as in our previous example –Answer : holding nothing initially. On each iteration of the loop, looking at the first element of values, and if that element is not the one we are trying to remove, setting answer to be (cons (first values) answer) That is, adding the value to the answer list. When do we stop? What answer do we return?

Remove-iterative (Defun remove-iterative(X L) (do ( ( (values L (rest values) ) ( (answer ‘() ) ) ( (null values) answers) ) ) (if (not (equal (first values) x)) (cons (first values) answer) answer) In this function, all the work is done by the if which decides whether to add the first element of the values list to the answer or not, depending on whether it is equal to the element X that to be removed.

EXERCISE The following pattern of numbers is called Pascal’s triangle: The numbers at the edge of the triangle are all 1 and each number inside the triangle is the sum of the two numbers above it. Write a procedure that computes elements of Pascal’s triangle by means of a recursive process