0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Advertisements

0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
Lists Samuel Marateck © The Sieve of Eratosthenes.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Laziness and Infinite Datastructures Koen Lindström Claessen.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Chapter 4 Number Theory. Terms Factors Divides, divisible, divisibility Multiple.
Chapter 14 Programming With Streams. Streams  A stream is an infinite sequence of values.  We could define a special data type for them: data Stream.
Comp 205: Comparative Programming Languages Lazy Evaluation Errors Evaluation Strategies Infinite Lists…. Lecture notes, exercises, etc., can be found.
Advanced Programming Handout 11 Programming With Streams (SOE Chapter 14)
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Sieve of Eratosthenes. The Sieve of Eratosthenes is a method that.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
Repetition Statements while and do while loops
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
CATCH 1 : Case and Termination Checking for Haskell Neil Mitchell (supervised by Colin Runciman) 1 Name courtesy of Mike Dodds.
Prime Numbers and Factoring. Which expression is equal to 4x6? (a) 2 × 2 x 2 (b) 2 × 3 × 4 (c) 4 × 5 × 3 (d) 6 × 6.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Advanced Functional Programming 2010
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Conditional Expressions
PROGRAMMING IN HASKELL
Chapter 4 Repetition Statements (loops)
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
Haskell Chapter 1, Part II.
PPL Lazy Lists.
Sieve of Eratosthenes.
PROGRAMMING IN HASKELL
Using The Sieve of Eratosthenes
PROGRAMMING IN HASKELL
Sieve of Eratosthenes.
AP Java Warm-up Boolean Array.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lazy Programming Lazy evaluation:
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Common Array Algorithms
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
?? Programming with Streams Reading assignment
Streams Contract is the same as pairs...
Presentation transcript:

0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton

Redex 1 A reducible expression (redex) is a function that can be applied to its arguments. The expression (1+2)*(3+4) is not reducible since both arguments must be evaluated before it can be applied. The redexs are (1+2) and (3+4) but not (1+2)*(3+4). But once (1+2) and (3+4) have been reduced to 3 and 7 respectively, 3*7 is a redex.

Outermost first Consider int :: Int inf = 1 + inf But Run it and get a stack overflow. > inf where inf = 1+inf ERROR - C stack overflow > fst (0, inf) where inf = 1 + inf 0 > snd (inf, 0) where inf = 1 + inf 0 fst (0, _) and snd (_, 0) are both reducible. No need to reduce the other tuple element. 2

3 Outermost first ones :: [Int] ones = 1 : ones Consider Run it and get an infinite list. (Use Actions -> stop to stop it.) > take 5 ones where ones = 1:ones [1,1,1,1,1] ones = [1, 1, …]

4 > take 5 ones = take 5 1: ones = take 5 1:1:ones = take 5 1:1:1:ones = take 5 1:1:1:1:ones = take 5 1:1:1:1:1:ones = [1, 1, 1, 1, 1] At this point take 5 xs can be evaluated. So it is. Inner expressions are evaluated only until outer expressions can be.

primes :: [Int] primes = seive [2.. ] sieve :: [Int]  [Int] sieve (p:xs) = p : sieve [x | x  xs, x `mod` p  0] 5 Note: [1.. ] = [1, 2, 3, 4, … ] > take 5 [1..] [1,2,3,4,5] Sieve of Eratosthenes. Generate the primes one by one, eliminating all multiples of generated primes. > take 5 primes where primes = sieve [2.. ]; sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0] [2,3,5,7,11] Primes

sieve (p:xs) = p : sieve [x | x  xs, x `mod` p  0] 6 Primes > take 4 primes = take 4 sieve [2.. ] = take 4 (2 : sieve [x | x  [3.. ], x `mod` 2  0]) = take 4 (2 : sieve (3: [x | x  [4.. ], x `mod` 2  0])) = take 4 (2 : 3 : sieve [y | y  [x | x  [4.. ], x `mod` 2  0], y `mod` 3  0] = take 4 (2 : 3 : sieve [y | y  [x | x  [5.. ], x `mod` 2  0], y `mod` 3  0] = take 4 (2 : 3 : sieve [y | y  5:[x | x  [6.. ], x `mod` 2  0], y `mod` 3  0] = take 4 (2 : 3 : sieve (5: [y | y  [x | x  [6.. ], x `mod` 2  0], y `mod` 3  0])) = take 4 (2 : 3 : 5 : sieve [z | z  [y | y  [x | x  [6.. ], x `mod` 2  0], y `mod` 3  0], z `mod` 5  0]) = take 4 (2 : 3 : 5 : sieve [z | z  [y | y  [x | x  [7.. ], x `mod` 2  0], y `mod` 3  0], z `mod` 5  0]) = take 4 (2 : 3 : 5 : sieve [z | z  [y | y  (7:[x | x  [8.. ], x `mod` 2  0]), y `mod` 3  0], z `mod` 5  0]) = take 4 (2 : 3 : 5 : sieve [z | z  7: [y | y  [x | x  [8.. ], x `mod` 2  0], y `mod` 3  0], z `mod` 5  0]) = take 4 (2 : 3 : 5 : sieve (7: [z | z  [y | y  [x | x  [8.. ], x `mod` 2  0], y `mod` 3  0], z `mod` 5  0])) = take 4 (2 : 3 : 5 : 7 : sieve [w | w  [z | z  [y | y  [x | x  [8.. ], x `mod` 2  0], y `mod` 3  0], z `mod` 5  0], w `mod` 7  0] ) = [2, 3, 5, 7]

Fibonacci numbers Exercise 4. > take 9 fibs where fibs = 0 : 1 : [x + y | (x, y) <- zip fibs (tail fibs)] [0,1,1,2,3,5,8,13,21] 7