Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25
|
|
- Elinor Lang
- 6 years ago
- Views:
Transcription
1 Homework Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25 Copyright c UMaine Computer Science Department 1 / 33 1
2 COS 140: Foundations of Computer Science Control Constructs: Conditionals and Loops Fall 2017 Introduction 3 Language constructs Why study them? Example Example Conditionals 8 If then else Example Syntax Blocks Other conditionals Loops 15 Loops Example For Loops While Loops Repeat until Loops Recursion 23 Example Why use it? Languages Cost A warning
3 Introduction 3 / 33 Programmimg Language Control Constructs In a program, often need to do one thing or another based on some condition do some statement or sets of statements multiple times Programming languages have constructs for controlling the execution of programs in these ways: conditionals and loops Copyright c UMaine Computer Science Department 3 / 33 3
4 Why Study Conditionals and Loops? Practical: need to use them Two important constructs that reflect how people solve problems Many ways these could be implemented in a programming language which is best? Copyright c UMaine Computer Science Department 4 / 33 Example: Counting Cards How would you program the computer to count cards of different colors? Assume that you can read information about a single card and find out its value and suit What variables are needed, and of what type? What instructions would you give? You can only use simple instructions; and You must specify everything that needs to be done. Copyright c UMaine Computer Science Department 5 / 33 4
5 A Solution Variables: Value, Suit: the value and suit as the information is read Red: number of red cards (integer) Black: number of black cards (integer) Instructions in pseudocode: Start: Open file containing information about the cards; Red = 0; Black = 0; Read information about a card into Value and Suit; Repeat until no more cards: If Suit is "clubs" or "spades" then Black = Black + 1; else Red = Red + 1; end if; Read info about a card into Value and Suit; end repeat; Print values of Red, Black; end program; Copyright c UMaine Computer Science Department 6 / 33 Example: Booth s Algorithm Conditionals needed: look for 0 1 or 1 0 in the last bit of Q and in Q-1, do something special in these cases Loops needed: repeat most of the steps in the algorithm for the number of bits in the register Copyright c UMaine Computer Science Department 7 / 33 5
6 Conditionals 8 / 33 If Then Else Construct The most widely-used construct for conditionals Example syntax: if condition then statement 1 else statement 2 The else part is usually optional Semantics: If condition is true, then do statement 1. Otherwise, do statement 2 (if the else is specified otherwise, do nothing) Then go to statement after the if then else. Note: statement 1 and statement 2 can be compound statements or blocks (later) Copyright c UMaine Computer Science Department 8 / 33 Example: If Then Else for Booth s Algorithm if (Q(last) = 1 & Q1 = 0) then A := A - M else if (Q(last) = 0 & Q1 = 1) then A := A + M; shiftright(q); Copyright c UMaine Computer Science Department 9 / 33 6
7 If Then Else Syntax Actual syntax will vary from language to language Examples: Pascal: if (Q(last) = 1 & Q1 = 0) then A := A - M else if (Q(last) = 0 & Q1 = 1) then A := A + M; shiftright(q); C: if (Q[last] == 1 && Q1 == 0) A = A - M; else if (Q[last] == 0 && Q1 == 1) A = A + M; shiftright(q); Copyright c UMaine Computer Science Department 10 / 33 If Then Else Syntax Lisp: Python: (if (= (car (last Q)) 1) (if (= Q1 0) (decf A M)) (if (= Q1 1) (incf A M))) (shiftright Q) if Q[last] == 1 and Q1 == 0: A = A - M else: if Q[last] == 0 and Q1 == 1: A = A + M shiftright(q) Copyright c UMaine Computer Science Department 11 / 33 7
8 Blocks Sometimes need to do several statements instead of just one for then or else Problem: How can the language tell how many statements go with those parts and where they end? Blocks group together statements make it clear where they begin and end Also called compound statements Possible delimiters for blocks: begin stmt stmt... stmt end {stmt stmt... stmt} if stmt stmt... stmt else stmt stmt... stmt endif if stmt stmt... stmt else stmt stmt... stmt fi do stmt stmt... stmt od spacing Copyright c UMaine Computer Science Department 12 / 33 Some Other Conditionals Case or switch statement (many languages) Have a variable with many possible values, each of which corresponds to a case of what should be done Each enumerated case has statements to be carried out in that case Example: case dayofweek of 0: write( Sunday ); 1: write( Monday );... end; Copyright c UMaine Computer Science Department 13 / 33 8
9 Some Other Conditionals Cond statement (Lisp) Can have an arbitrary number of conditions associated with actions to be taken Do the actions associated with the first true condition Example: (cond ((member name (alice bob carol)) (print "CS professor")) ((member name (david elizabeth frank)) (print "IS professor")) (t (print "Unknown"))) Copyright c UMaine Computer Science Department 14 / 33 Loops 15 / 33 Loops Loop construct supports iteration allows same statement(s) to be executed repeatedly Several types of loops reflects that the number of iterations can be determined in different ways for different situations Copyright c UMaine Computer Science Department 15 / 33 9
10 Example: Booth s Algorithm Need to do the core of the algorithm ( <core> ) the same number of times as the number of bits in the multiplier for bit=1 to C do <core> Particularly nice if need to use bit for some reason in program while c>0 do <core> C:=C-1; end Here, programmer must change the value of C Copyright c UMaine Computer Science Department 16 / 33 For Loops Syntax: for iterationvariable = lowerbound to upperbound do statementorblock Iteration variable gets set to the lower bound first time through the loop Each time the statement(s) in the loop is (are) executed, iteration variable incremented by one (Some languages: can decrement or specify amount of increment/decrement.) When the iteration variable exceeds upper bound, stop executing loop Control then goes to the following statement Copyright c UMaine Computer Science Department 17 / 33 10
11 For Loops Syntax varies from language to language Pascal: C: for assignment := 1 to 20 do sum := sum + grades[assignment]; average := sum / 20; for (assignment=1;assignment<=20;assignment++) sum = sum + grades[assignment]; average = sum/20; Copyright c UMaine Computer Science Department 18 / 33 For Loops Python: for i in range(20): sum = sum + grades[i] average = sum / 20 Lisp: (dotimes (assignment 20) (setf sum (+ sum (aref grades (1- assignment))))) (setf average (/ sum 20.0)) OR (setf average (/ (loop for assignment from 0 to 19 sum (aref grades assignment)) 20.0)) OR (setq average (/ (reduce # + assignment) 20.0)) Copyright c UMaine Computer Science Department 19 / 33 11
12 While Loops Syntax: while condition do statementorblock As long as condition is true, execute the statement(s) in loop Programmer is responsible for ensuring that something changes so that the condition will eventually be false otherwise, have an infinite loop Copyright c UMaine Computer Science Department 20 / 33 While loop examples Pascal numgrades := 0; write( Enter grade (-1 to quit): ); readln(grade); while grade >= 0 do begin sum := sum + grade; numgrades := numgrades + 1; write( Enter grade: ); readln(grade) end; average := sum / numgrades; Python C printf("enter grade (-1 to quit): "); grade = scanf("%f"); while (grade >= 0) { sum = sum + grade; numgrades++; printf("enter grade: "); grade = scanf("%f"); } average = sum/numgrades; Lisp grade = input("enter grade (-1 to quit): ") (loop with grade while grade >=0: with num-grades = 0 sum = sum + grade with sum = 0 numgrades = numgrades + 1; do (princ "Enter grade (-1 to quit): ") grade = input("enter grade (-1 to quit): ") while (>= (setq grade (read)) 0) average = sum / numgrades do (incf sum grade) (incf num-grades) finally (return (/ sum num-grades))) Copyright c UMaine Computer Science Department 21 / 33 12
13 Repeat until Loops Syntax varies, but often: repeat statementorblock until condition As long as condition is false, execute the statement(s) in loop Unlike while loop, body will be executed 1 times Like while loop, programmer is responsible for ensuring that something changes so that the condition will eventually be false otherwise, have an infinite loop Copyright c UMaine Computer Science Department 22 / 33 Recursion 23 / 33 Recursion Loops are iteration constructs Iteration isn t only way to repeat things Sometimes more natural to use recursion: Cast problem as some operation + simpler version of the problem Copyright c UMaine Computer Science Department 23 / 33 13
14 Example Common, simple example: factorial n! = (n 1) n (1) = n (n 1) (n 2) (2) = n [(n 1) (n 2) ] (3) = n (n 1)! (4) This is a very natural way to think of the problem, and to program Copyright c UMaine Computer Science Department 24 / 33 Recursion Need to specify a base case: when the recursion stops: e.g., 1! = 1 Need to specify a recursive step here, n (n 1)! Pseudocode: subroutine factorial(n): if n = 1 then return 1; else return n * factorial(n-1); fi; end factorial; Copyright c UMaine Computer Science Department 25 / 33 14
15 Why use it? Sometimes more natural than loops Examples: Search for a value in a sorted list Search a tree Play a game of chess Create a plan to go from here to Kapa a, Hawaii Copyright c UMaine Computer Science Department 26 / 33 Languages Most languages support recursion (exceptions include the original FORTRAN) Functional languages: basic construct for repeating E.g., Lisp family of languages: (defun factorial (n) (cond ((<= n 1) 1) (t (* n (factorial (1- n)))))) Copyright c UMaine Computer Science Department 27 / 33 15
16 Cost of Recursion Some think recursion is slow: Loops: require only a jump instruction to do next iteration Recursion: each recursive call requires a function call, which can be expensive However, it is as fast as loops for tail recursion: A function is tail-recursive if the last thing the function does before returning is the recursive call Languages are optimized for this: they don t do a subroutine call, just do body of function again without creating new stack frame Almost everything can be made tail-recursive using dynamic programming Copyright c UMaine Computer Science Department 28 / 33 Tail Recursion Tail-recursive factorial: (defun factorial (n &optional (so-far 1)) (cond ((<= n 1) so-far) (t (factorial (1- n) (* n so-far))))) Faster even for this simple case: run 1,000,000 times in Lisp: Non-tail-recursive: 4.47 seconds Tail-recursive: 4.40 seconds Speed up of 1.6% Copyright c UMaine Computer Science Department 29 / 33 16
17 When not to use recursion Consider the case of the Fibonacci sequence: F(n) = F(n 1)+F(n 2) This is a straightforward recursive definition Pseudocode: Subroutine Fib(n): if n < 2 then return 1; else return Fib(n-1) + Fib(n-2); fi; end; Difficult to make tail-recursive; but not the worst problem Recursive calls form a tree, branching factor 2: exponential Copyright c UMaine Computer Science Department 30 / 33 n calls When not to use recursion , , ,664, ,668,309 Copyright c UMaine Computer Science Department 31 / 33 17
18 What to do instead? Dynamic programming or use a loop Non-tail recursive Fibonacci of n = 50 in Lisp: 21 minutes Tail-recursive or iterative Fibonacci of n = 10, 000: about 180 milliseconds Copyright c UMaine Computer Science Department 32 / 33 Fibonacci By the way, in case anyone asks, Fibonacci(10,000) = Copyright c UMaine Computer Science Department 33 / 33 18
Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25
Homework Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 33 COS
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationCommon LISP Tutorial 1 (Basic)
Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)
More informationLECTURE 18. Control Flow
LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a
More informationModern Programming Languages. Lecture LISP Programming Language An Introduction
Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation
More informationFall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationSCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017
SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationStructure of Programming Languages Lecture 5
Structure of Programming Languages Lecture 5 CSCI 6636 4536 June, 2017 CSCI 6636 4536 Lecture 10... 1/16 June, 2017 1 / 16 Outline 1 Expressions and Evaluation 2 Control Structures Conditionals Repetition
More informationFall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationSCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012
SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually
More informationRecursion & Iteration
Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion
More informationRECURSION, RECURSION, (TREE) RECURSION! 3
RECURSION, RECURSION, (TREE) RECURSION! 3 COMPUTER SCIENCE 61A September 18, 2013 A function is recursive if it calls itself. Below is recursive factorial function. def factorial(n): if n == 0 or n ==
More informationAn introduction to Scheme
An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize
More informationSpring 2018 Discussion 7: March 21, Introduction. 2 Primitives
CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme
More informationChapter 8. Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions 1-2 Levels of Control Flow Within
More informationChapter 8. Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2009 Addison-Wesley.
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More informationCS1 Recitation. Week 2
CS1 Recitation Week 2 Sum of Squares Write a function that takes an integer n n must be at least 0 Function returns the sum of the square of each value between 0 and n, inclusive Code: (define (square
More informationAllegro CL Certification Program
Allegro CL Certification Program Lisp Programming Series Level I Review David Margolies 1 Summary 1 A lisp session contains a large number of objects which is typically increased by user-created lisp objects
More informationMore Complicated Recursion CMPSC 122
More Complicated Recursion CMPSC 122 Now that we've gotten a taste of recursion, we'll look at several more examples of recursion that are special in their own way. I. Example with More Involved Arithmetic
More informationAPCS-AB: Java. Recursion in Java December 12, week14 1
APCS-AB: Java Recursion in Java December 12, 2005 week14 1 Check point Double Linked List - extra project grade Must turn in today MBCS - Chapter 1 Installation Exercises Analysis Questions week14 2 Scheme
More informationRECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012
RECURSION 7 COMPUTER SCIENCE 61A October 15, 2012 1 Recursion We say a procedure is recursive if it calls itself in its body. Below is an example of a recursive procedure to find the factorial of a positive
More informationRECURSION AND LINKED LISTS 3
RECURSION AND LINKED LISTS 3 COMPUTER SCIENCE 61A July 1, 2014 1 Termination and Warmup A function is recursive if executing a call to the function requires another call to the same function, called a
More informationAlgorithmics. Some information. Programming details: Ruby desuka?
Algorithmics Bruno MARTIN, University of Nice - Sophia Antipolis mailto:bruno.martin@unice.fr http://deptinfo.unice.fr/~bmartin/mathmods.html Analysis of algorithms Some classical data structures Sorting
More informationChapter 8 Algorithms 1
Chapter 8 Algorithms 1 Objectives After studying this chapter, the student should be able to: Define an algorithm and relate it to problem solving. Define three construct and describe their use in algorithms.
More informationFunctions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Functions CS10001: Programming & Data Structures Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Recursion A process by which a function calls itself
More informationSCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015
SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationLoops / Repetition Statements
Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for
More informationRECURSION, RECURSION, (TREE) RECURSION! 2
RECURSION, RECURSION, (TREE) RECURSION! 2 COMPUTER SCIENCE 61A February 5, 2015 A function is recursive if it calls itself. Below is a recursive factorial function. def factorial(n): if n == 0 or n ==
More information6.001 Notes: Section 4.1
6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More information2.2 (a) Statement, subroutine, procedure, function, parameter, loop
Chapter 2.2: The structure of procedural programs 2.2 (a) Statement, subroutine, procedure, function, parameter, loop Procedural programs are ones in which instructions are executed in the order defined
More informationChapter 6 Control Flow. June 9, 2015
Chapter 6 Control Flow June 9, 2015 Expression evaluation It s common in programming languages to use the idea of an expression, which might be a simple object function invocation over some number of arguments
More informationChapter 8 Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence rules, was discussed. This chapter discusses
More information8 Algorithms 8.1. Foundations of Computer Science Cengage Learning
8 Algorithms 8.1 Foundations of Computer Science Cengage Learning 8.2 Objectives After studying this chapter, the student should be able to: Define an algorithm and relate it to problem solving. Define
More informationCS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.
CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3
More informationRecursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures. Recursive design. Convert loops to recursion
Recursion(int day){return Recursion(day += 1);} Comp Sci 1575 Data Structures Outline 1 2 Solution 2: calls 3 Implementation To create recursion, you must create recursion. How to a recursive algorithm
More informationINF4820. Common Lisp: Closures and Macros
INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures
More informationRecursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem
Recursion Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem What is Recursion? A problem is decomposed into smaller sub-problems, one or more of which are simpler versions of
More informationCSCI 2212: Intermediate Programming / C Recursion
... 1/40 CSCI 2212: Intermediate Programming / C Recursion Alice E. Fischer November 13 and 16, 2015 ... 2/40 Outline What is Recursion? Recursively Defined Images What is a Recursive Function? How Does
More informationNonvisual Arrays and Recursion. by Chris Brown under Prof. Susan Rodger Duke University June 2012
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012 Nonvisual Arrays This tutorial will display how to create and use nonvisual arrays in Alice. Nonvisual arrays
More informationCSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion
CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion Announcements: Test 1 Information Test 1 will be held Monday, Sept 25th, 2017 from 6-7:50pm Students will be randomly assigned
More informationRecursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms
Recursive Methods and Problem Solving Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Review: Calling Methods int x(int n) { int m = 0; n = n + m + 1; return n; int y(int
More informationPutting the fun in functional programming
CM20167 Topic 4: Map, Lambda, Filter Guy McCusker 1W2.1 Outline 1 Introduction to higher-order functions 2 Map 3 Lambda 4 Filter Guy McCusker (1W2.1 CM20167 Topic 4 2 / 42 Putting the fun in functional
More informationCh. 7: Control Structures
Ch. 7: Control Structures I. Introduction A. Flow of control can be at multiple levels: within expressions, among statements (discussed here), and among units. B. Computation in imperative languages uses
More informationProgramming for Electrical and Computer Engineers. Loops
Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly
More informationRecursive Definitions
Recursion Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of
More informationCS159. Nathan Sprague. November 9, 2015
CS159 Nathan Sprague November 9, 2015 Recursive Definitions Merriam Websters definition of Ancestor: Ancestor One from whom a person is descended [...] Here is a recursive version: Ancestor One s parent.
More informationRECURSION, RECURSION, (TREE) RECURSION! 3
RECURSION, RECURSION, (TREE) RECURSION! 3 COMPUTER SCIENCE 61A September 18, 2013 A function is recursive if it calls itself. Below is recursive factorial function. def factorial(n): if n == 0 or n ==
More informationIntroduction to the Analysis of Algorithms. Algorithm
Introduction to the Analysis of Algorithms Based on the notes from David Fernandez-Baca Bryn Mawr College CS206 Intro to Data Structures Algorithm An algorithm is a strategy (well-defined computational
More informationWriting Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters
What is a function? Function declaration and parameter passing Return values Objects as parameters Reading: Writing Functions Dawson, Chapter 6 Abstraction Data scoping Encapsulation Modules Recursion
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More information6.001 Notes: Section 15.1
6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define
More informationAnnouncement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.
Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also
More informationFifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application
Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer
More informationChapter 8. Statement-Level Control Structures ISBN
Chapter 8 Statement-Level Control Structures ISBN 0-321-49362-1 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2012
More informationAlgorithms. Chapter 8. Objectives After studying this chapter, students should be able to:
Objectives After studying this chapter, students should be able to: Chapter 8 Algorithms Define an algorithm and relate it to problem solving. Define three construct and describe their use in algorithms.
More informationProgram Syntax; Operational Semantics
9/5 Solved Program Syntax; Operational Semantics CS 536: Science of Programming, Fall 2018 A. Why Our simple programming language is a model for the kind of constructs seen in actual languages. Step-by-step
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationDecisions, Decisions. Testing, testing C H A P T E R 7
C H A P T E R 7 In the first few chapters, we saw some of the basic building blocks of a program. We can now make a program with input, processing, and output. We can even make our input and output a little
More informationCSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp
CSE 341 Lecture 5 efficiency issues; tail recursion; print Ullman 3.3-3.4; 4.1 slides created by Marty Stepp http://www.cs.washington.edu/341/ Efficiency exercise Write a function called reverse that accepts
More informationRecursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?
Recursion Chapter 8 CS 3358 Summer I 2012 Jill Seaman What is recursion? Generally, when something contains a reference to itself Math: defining a function in terms of itself Computer science: when a function
More informationLOOPS. Repetition using the while statement
1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while
More information61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013
61A LECTURE 22 TAIL CALLS, ITERATORS Steven Tang and Eric Tzeng July 30, 2013 Announcements Homework 12 due Thursday, not Monday. Take the time to get started on the project instead! Almost done with Scheme
More informationRecursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences
Section 5.3 1 Recursion Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences Previously sequences were defined using a specific formula,
More informationChapter 5: Recursion
Chapter 5: Recursion Objectives Looking ahead in this chapter, we ll consider Recursive Definitions Function Calls and Recursive Implementation Anatomy of a Recursive Call Tail Recursion Nontail Recursion
More informationCSCI 3155: Principles of Programming Languages Exam preparation #1 2007
CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 Exercise 1. Consider the if-then-else construct of Pascal, as in the following example: IF 1 = 2 THEN PRINT X ELSE PRINT Y (a) Assume
More informationDesign and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis
Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Week 02 Module 06 Lecture - 14 Merge Sort: Analysis So, we have seen how to use a divide and conquer strategy, we
More informationFunctional programming with Common Lisp
Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions
More informationCMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi
CMPT 120 Control Structures in Python Summer 2012 Instructor: Hassan Khosravi The If statement The most common way to make decisions in Python is by using the if statement. The if statement allows you
More informationPIC 10A Flow control. Ernest Ryu UCLA Mathematics
PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if
More informationIterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure
ICOM 4036 Programming Languages Statement-Level Control Structure Selection Statement Iterative Statements Unconditional Branching Guarded Commands This lecture covers review questions 8-16 This lecture
More informationSCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))
SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if
More informationLecture 10: Recursive Functions. Computer System and programming in C 1
Lecture 10: Recursive Functions Computer System and programming in C 1 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive
More informationLists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3
Lists of Lists CS 5010 Program Design Paradigms Bootcamp Lesson 5.3 Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1 Learning
More informationCS 415 Midterm Exam Fall 2003
CS 415 Midterm Exam Fall 2003 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to
More informationAlgorithm Design and Recursion. Search and Sort Algorithms
Algorithm Design and Recursion Search and Sort Algorithms Objectives To understand the basic techniques for analyzing the efficiency of algorithms. To know what searching is and understand the algorithms
More informationCS 842 Ben Cassell University of Waterloo
CS 842 Ben Cassell University of Waterloo Recursive Descent Re-Cap Top-down parser. Works down parse tree using the formal grammar. Built from mutually recursive procedures. Typically these procedures
More informationFunctional Programming
Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used
More informationINF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp
INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017 Agenda 2 Previously Common
More informationSolving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3
Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3 Ziad Matni Dept. of Computer Science, UCSB A Word About Registration for CS16 FOR THOSE OF YOU NOT YET REGISTERED:
More informationDesign and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization
Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Module 02 Lecture - 45 Memoization Let us continue our discussion of inductive definitions. (Refer Slide Time: 00:05)
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 9 (Part II) Recursion MOUNA KACEM Recursion: General Overview 2 Recursion in Algorithms Recursion is the use of recursive algorithms to solve a problem A recursive algorithm
More informationChapter Fourteen Bonus Lessons: Algorithms and Efficiency
: Algorithms and Efficiency The following lessons take a deeper look at Chapter 14 topics regarding algorithms, efficiency, and Big O measurements. They can be completed by AP students after Chapter 14.
More informationIdentify recursive algorithms Write simple recursive algorithms Understand recursive function calling
Recursion Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling With reference to the call stack Compute the result of simple recursive algorithms Understand
More informationChapter 8. Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures Levels of Control Flow Within expressions Among program units Among program statements Copyright 2012 Addison-Wesley. All rights reserved. 1-2 Control Structure
More informationCS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014
CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the
More informationCOMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 13: Recursion Sandeep Manjanna, Summer 2015 Announcements Final exams : 26 th of June (2pm to 5pm) @ MAASS 112 Assignment 4 is posted and Due on 29 th of June
More informationLists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5
Lists of Lists CS 5010 Program Design Paradigms Bootcamp Lesson 6.5 Mitchell Wand, 2012-2015 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1 Learning
More informationCIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers
CIS 194: Homework 6 Due Monday, February 25 Files you should submit: Fibonacci.hs This week we learned about Haskell s lazy evaluation. This homework assignment will focus on one particular consequence
More informationChapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.
Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop
More informationAn Introduction to Python
An Introduction to Python Day 2 Renaud Dessalles dessalles@ucla.edu Python s Data Structures - Lists * Lists can store lots of information. * The data doesn t have to all be the same type! (unlike many
More informationCSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion
CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion Review from Lectures 5 & 6 Arrays and pointers, Pointer arithmetic and dereferencing, Types of memory ( automatic, static,
More information1 Lower bound on runtime of comparison sorts
1 Lower bound on runtime of comparison sorts So far we ve looked at several sorting algorithms insertion sort, merge sort, heapsort, and quicksort. Merge sort and heapsort run in worst-case O(n log n)
More informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to
More informationHandout 9: Imperative Programs and State
06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 9: Imperative Programs and State Imperative
More informationCIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL
CIS 194: Homework 3 Due Wednesday, February 11, 2015 Interpreters An interpreter is a program that takes another program as an input and evaluates it. Many modern languages such as Java 1, Javascript,
More informationITERATION AND RECURSION 3
ITERATION AND RECURSION 3 COMPUTER SCIENCE 61A June 26, 2012 1 Newton s Method Newton s method is an algorithm that is widely used to compute the zeros of functions. It can be used to approximate a root
More informationReduction & Recursion Overview
Reduction & Recursion Overview Reduction definition Reduction techniques Recursion definition Recursive thinking (Many) recursion examples Indirect recursion Runtime stack Factorial isnumericstring add
More information