Lecture 15: Iteration and Recursion

Similar documents
Lecture 13: Expression Evaluation

LECTURE 18. Control Flow

G Programming Languages - Fall 2012

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Lecture 31: Building a Runnable Program

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

CSI32 Object-Oriented Programming

Programming Languages 2nd edition Tucker and Noonan"

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS558 Programming Languages

Concepts of Programming Languages

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

2/20/2018. Chapter 6:: Control Flow. control flow or ordering. basic categories for control flow (cont.): basic categories for control flow:

Control Flow. Stephen A. Edwards. Fall Columbia University

Comp 333: Concepts of Programming Languages Fall 2016

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Expressions vs statements

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 4: Syntax Specification

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Principles of Programming Languages Lecture 22

The role of semantic analysis in a compiler

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Lecture 10: Recursion vs Iteration

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Functional Programming

LECTURE 17. Expressions and Assignment

Control Flow February 9, Lecture 7

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Stop coding Pascal. Saturday, April 6, 13

Introduction to Scientific Computing Languages

Programming Languages, Summary CSC419; Odelia Schwartz

CSCI 3136 Principles of Programming Languages

Introduction to Scientific Computing Languages

G Programming Languages - Fall 2012

Programming Languages: Lecture 11

CS 314 Principles of Programming Languages

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction. A. Bellaachia Page: 1

COMPILER DESIGN LECTURE NOTES

CS558 Programming Languages

Chapter 6 Control Flow. June 9, 2015

Chapter 7 Control I Expressions and Statements

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Chapter 8. Statement-Level Control Structures

MIDTERM EXAM (Solutions)

CST-402(T): Language Processors

CS 415 Midterm Exam Spring 2002

Functional Programming. Big Picture. Design of Programming Languages

22c:111 Programming Language Concepts. Fall Types I

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

LECTURE 1. Overview and History

Programming Language Pragmatics

Types II. Hwansoo Han

CSCI312 Principles of Programming Languages!

HANDLING NONLOCAL REFERENCES

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

Working of the Compilers

CS558 Programming Languages

Lecture 16: Static Semantics Overview 1

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

INSTITUTE OF AERONAUTICAL ENGINEERING

Lecture 13: Complex Types and Garbage Collection

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Structure of Programming Languages Lecture 5

Programmiersprachen (Programming Languages)

Chapter 8 Statement-Level Control Structures

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Example Scheme Function: equal

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Control Flow. CSE 307 Principles of Programming Languages Stony Brook University

NOTE: Answer ANY FOUR of the following 6 sections:

CSCI 2041: Advanced Language Processing

Computer Components. Software{ User Programs. Operating System. Hardware

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Principles of Programming Languages. Lecture Outline

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

15. Functional Programming

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Concepts of Programming Languages

Programming Languages

Why are there so many programming languages?

Logic Programming II & Revision

Chapter 8. Statement-Level Control Structures

Transcription:

Lecture 15: and Recursion The University of North Carolina at Chapel Hill Spring 2002 Lecture 15: and Recursion Feb 13/15 1 Control Flow Mechanisms Sequencing Textual order, Precedence in Expression Selection Procedural abstraction Recursion Concurrency Nondeterminacy 2 1

Lecture 15: and Recursion and Recursion These two control flow mechanism allow a computer to perform the same set of operations repeatedly They make computers useful Go beyond the power of deterministic finite automata Imperative languages mainly rely on iterations Functional languages make more use of recursion 3 usually takes the form of loops There are two principal varieties Enumeration-controlled loops» E.g. for (int i = 0; i <= 10; i++) { } Logically controlled loops» E.g. int i = 0; while (i <= 10) { i++; } 4 2

Lecture 15: and Recursion Enumeration-controlled loops Enumeration-controlled loops Index variable Step size and bounds Body of the loop Fortran I, II and IV do 10 i = 1, 10, 2... 10: continue The value of i is tested at the end of the loop» When continue is reached Implementation is very fast» This statement is very close to assembly code 5 Enumeration-controlled loops Problems: Loop boundaries must be integer» Expressions are not allowed The index variable can change within the body of the loop Goto statements may jump in and out of the loop The value of i after the termination of the loop is implementation dependent The test of the loop takes place at the end, so the body is executed at least once» Even if the lower bound is larger than the upper bound! 6 3

Lecture 15: and Recursion Loop should check for empty bounds Code generation Optimization 7 Backward loops Previous code assumed a positive step size 8 4

Lecture 15: and Recursion Access to Index Outside the Loop The value of the index variable at the end of loop is undefined in several languages E.g. Fortran, Pascal Compilers can fix this, but Generating slower code 9 Access to Index Outside the Loop The value of the index after the loop completes may not be valid E.g. var c: a.. z ; for c:= a to z do begin end; (* what comes after z? *) In summary, even the simplest type of loop requires a good design You will use language with poorly designed statements! 10 5

Lecture 15: and Recursion Iterators Iterators generalize enumeration-controlled loops In the previous examples, the iteration was always over the elements of an arithmetic sequence Iterators are used to enumerate the elements of any well-defined set E.g. In Clu, for i in from_to_by(first, last, step) do end Notice the similarity with Python s range 11 Iterators Clu allows any set-like abstract data type to provide an iterator E.g. integer iterator 12 6

Lecture 15: and Recursion Iterators E.g. Binary tree iterator in Clu 13 s Iterators Python will support generators/iterators in the future http://www.python.org/doc/current/ref/yield.html http://python.sourceforge sourceforge.net/peps/pep-0255.html Iterators can also be based on object-oriented oriented design patterns Java s Iterator interface» http://java.sun.com/docs/books/tutorial/collections/interfaces/colle olle ction.html Notice that the loop statement is a logically-controlled controlled one, but it is used for an enumeration-controlled task Enumeration-controlled loops evolved significantly since FORTRAN s original for 14 7

Lecture 15: and Recursion Logically-Controlled Loops They have fewer semantic subtleties The programmer has to be more explicit There are some design options Pre-test» http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#237277 Post-test test» http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6045 Midtest» C/C++/Java idiom: for (;;) {... if (condition) break... } 15 Recursion Recursion requires no special syntax Recursion and logically-controlled controlled iteration are equally powerful Example Compute the greatest common divisor It can be defined as a recurrence: for a, b positive integers a if a = b gcd( a, b) gcd( a b, b) if a > b gcd( a, b a ) if a < b 16 8

Lecture 15: and Recursion Recursion Implementation using recursion is direct Recursion a gcd( a, b) gcd( a b, b) gcd( a, b a) if if if a = b a > b a < b 17 Recursion We will discuss other control flow issues in the Haskell lectures Applicative-order evaluation vs. normal-order order evaluation Impatient evaluation vs. lazy evaluation 18 9

Lecture 15: and Recursion Nondeterminacy Nondeterministic constructs make choices between alternatives deliberately unspecified This mechanism is specially useful in concurrent programs Message-based concurrent languages We will discuss concurrent programming later in this class 19 Nondeterminacy This is a very practical matter Event-driven programming is related to nondeterminacy» See events and listeners in Java» http://java.sun.com/docs/books/tutorial/uiswing/overview/event.ht ml Non-blocking IO is related to nondeterminacy» See the lastest addition to Java (1.4): Channels and Selectors» http://java.sun.com/j2se/1.4/docs/guide/nio/index.html 20 10

Lecture 15: and Recursion Reading Assignment Scott s chapter 6 Section 6.5, except» Combination loops» Generators in Icon» Enumerating without Iterators Section 6.6» First page and a half» We will discuss the rest in the context of Haskell (instead of Lisp) L 21 11