1 / 8

PROGRAMMING IN HASKELL

PROGRAMMING IN HASKELL. Chapter 12 – Lazy evaluation and infinite lists. Slides not from Hutton. Redex. A reducible expression (redex) is a function that can be applied to its arguments.

evania
Download Presentation

PROGRAMMING IN HASKELL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Redex • 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.

  3. Outermost first Consider Run it and get a stack overflow. int :: Int inf = 1 + inf > inf where inf = 1+inf ERROR - C stack overflow But > 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.

  4. Outermost first Consider ones :: [Int] ones = 1 : ones ones = [1, 1, …] 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]

  5. > 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.

  6. Primes > take 5 [1 ..] [1,2,3,4,5] Note: [1 .. ] = [1, 2, 3, 4, … ] primes :: [Int] primes = seive [2 .. ] sieve :: [Int]  [Int] sieve (p:xs) = p : sieve [x | x  xs, x `mod` p  0] 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]

  7. Primes The color coding lets you follow each list. [2 .. ] = 2 : [3 .. ] > take 4 primes = take 4 sieve [2 .. ] = take 4 sieve (2:[3 .. ]) = 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] sieve (p:xs) = p : sieve [x | x xs, x `mod` p 0]

  8. 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]

More Related