Topic 8: Lazy Evaluation

Similar documents
Topic 2: Making Decisions

Topic 2: Making Decisions

Topic 7: Algebraic Data Types

Lecture 5: Lazy Evaluation and Infinite Data Structures

Textbook. Topic 6: Functions. Motivation. What is a Function? What s a function? How can we use functions to write better software?

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Topic 9: Type Checking

Topic 9: Type Checking

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Exercise: Inventing Language

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Communication With the Outside World

Topic 4: Tuples and Lists

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

CE221 Programming in C++ Part 1 Introduction

Array Elements as Function Parameters

a data type is Types

C:\Temp\Templates. Download This PDF From The Web Site

Chapter Four: Loops II

Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

2 2

primitive arrays v. vectors (1)

Topic 1: Introduction

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Lecture 15a Persistent Memory & Shared Pointers

C++ for Java Programmers

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

C++ Namespaces, Exceptions

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

CS2141 Software Development using C/C++ C++ Basics

JVM ByteCode Interpreter

My First Command-Line Program

C++ Programming Fundamentals

C++ Lab 03 - C++ Functions

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

More on Strings & Arrays

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

5. Control Statements

Assumptions. History

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Module 06. Topics: Iterative structure in Python Readings: ThinkP 7. CS116 Spring : Iteration

Reversing. Time to get with the program

Streams and Lazy Evaluation in Lisp

Evaluating Bug Finders

Prime Factorization. Jane Alam Jan. 1 P a g e Document prepared by Jane Alam Jan

Chapter Two MIPS Arithmetic

Parameter Passing Styles

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Lab Instructor : Jean Lai

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Lecture 10 September 11, 2017

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

CPSC 427: Object-Oriented Programming

LAB 4.1 Relational Operators and the if Statement

CPSC 427: Object-Oriented Programming

Linked List using a Sentinel

cs1114 REVIEW of details test closed laptop period

Where do we go from here?

Pointers and scanf() Steven R. Bagley

Review of the C Programming Language

EE 109 Lab 8a Conversion Experience

Chapter 1 - What s in a program?

Pipelines, Forks, and Shell

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Memory, Arrays, and Parameters

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CS 106 Introduction to Computer Science I

Friend Functions and Friend Classes

Object-Oriented Design Lecture 13 CSU 370 Fall 2008 (Pucella) Friday, Oct 31, 2008

PIETRO, GIORGIO & MAX ROUNDING ESTIMATING, FACTOR TREES & STANDARD FORM

The following program computes a Calculus value, the "trapezoidal approximation of

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

CSI33 Data Structures

CSCI 2041: Lazy Evaluation

Computer Systems C S Cynthia Lee

Evolution of Programming Languages

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Recursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5

An introduction to C++ template programming

Homework 4. Any questions?

TOPIC 8 MORE ON WHILE LOOPS

Buffer overflow prevention, and other attacks

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Transcription:

Topic 8: Lazy Evaluation 1

Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 17.1, 17.2, 17.4, 17.8, 17.23, 17.25, 17.28, 17.29 Readings: Chapter 17.1, 17.2, 17.3, 17.6, 17.7 2

Lazy Evaluation When lazy evaluation is employed Objects and expressions are evaluated when they are needed Expressions that are not used subsequently are not evaluated Haskell is lazy In Haskell, lazy evaluation applies to Function parameters List elements Guards Logical expressions 3

Lazy Evaluation and Guards Consider the following function and function calls choice :: Int > a > a > a > a choice n x y z n > 0 = x n == 0 = y otherwise = z choice 10 (naivefib 29) (naivefib 30) (naivefib 31) choice 0 (naivefib 29) (naivefib 30) (naivefib 31) choice 2 (naivefib 29) (naivefib 30) (naivefib 31) 4

Lazy Evaluation and Pattern Matching Consider the following function and function calls multiply :: Int > Int > Int multiply 0 _ = 0 multiply _ 0 = 0 multiply x y = x * y multiply (12 + 12) (15 * 15) multiply 0 (naivefib 40) multiply (naivefib 40) 0 5

Lazy Evaluation With lazy evaluation Parameters are passed to the function without being evaluated Parameters are evaluated in the called scope only as required Repeated uses of the same parameter in the called scope are only evaluated once Composite parameters, such as lists and tuples, may only be partially evaluated 6

Evaluation in Java, C++ and Python In almost all circumstances, evaluation is eager Expressions are fully evaluated in the calling scope before they are passed to the function long nfib(long n) { if (n == 0) return 0; if (n == 1) return 1; return nfib(n 1) + nfib(n 2); } int main(int argc, char** argv) { cout << "Starting..." << endl; cout << " 0 * nfib(41) is " << multiply(0, nfib(41)) << endl; cout << " nfib(42) * 0 is " << multiply(nfib(42), 0) << endl; cout << " nfib(39) * nfib(40) is " << multiply(nfib(39), nfib(40)) << endl; long multiply(long x, long y) { if (x == 0) return 0; if (y == 0) return 0; return x * y; } return 0; } 7

Evaluation in Java, C++ and Python In C++, the optimizer can (sometimes) avoid fully evaluating expressions that are never used subsequently Mark nfib as a const function long nfib(long n) attribute ((const)); Turn on optimization g++ O lazy.cpp This is a rather different (and limited) form of lazy evaluation from what Haskell provides, but it provides some of the same benefits 8

Evaluation in Java, C++ and Python Exception to eager evaluation: Built in logical operators The and and or operators in Java, C++, C and Python all use a limited form of lazy evaluation that is often referred to as short circuit evaluation If the left operand s value conclusively determines the outcome of the logical expression then the right operand is not evaluated No matter what even if it has side effects! 9

Example: Prime Numbers How can we efficiently identify the first n prime numbers? Possible strategy: Start at two and check each number in sequence If the number is prime, add it to the list of primes Otherwise discard the number Repeat until n prime numbers have been identified What s undesirable about this solution? 10

Sieve of Eratosthenes An efficient technique for identifying all prime numbers from 2..n Starting from the beginning of the list Repeat Identify the next item in the list as prime Remove all multiples of the first element from consideration Until the next item is greater than or equal to n 11

Sieve of Eratosthenes 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 12

Sieve of Eratosthenes Implement the Sieve of Eratosthenes as a function Parameter: n (an integer), the maximum value to test Return: a list of primes (integers) less than or equal to n 13

Sieve of Eratosthenes But recall that the problem we really want to solve is finding a certain number of primes Because primes are not predictably spaced, we don t know how many primes we will get when finding all primes from 2 up to n We could pick some value for n and hope that it is big enough; or We could use an infinite list 14

Infinite Data Structures Lazy evaluation permits infinitely large data structures As long as they are never fully evaluated 15

Summary Evaluation in Haskell is performed differently than languages like C++, Java and Python Evaluation is lazy rather than eager Expressions are not evaluated until they are needed Unused expressions are never evaluated Lazy evaluation permits the creation of (but not complete evaluation of) infinite data structures 16

Infinite Data Structures Another example: Pseudo random numbers 17