1 / 14

Lazy Programming

Lazy Programming. Lazy evaluation: an expression is evaluated if and only if its value is needed to compute the overall result Consequences: elements are generated on demand so that complex intermediate structures are not necessarily expensive, e.g., an intermediate list ,

vian
Download Presentation

Lazy Programming

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. Lazy Programming Lazy evaluation: • an expression is evaluated if and only if its value is needed to compute the overall result Consequences: • elements are generated on demand so that complex intermediate structures are not necessarily expensive, e.g., an intermediate list , • expression may describe infinite data structures, e.g, the infinite list of all prime numbers, • implications to program design.

  2. Example h x y = x+x h (9-3) (h 12 15) • (9-3) + (9-3) • 6+6 • 12 + (9-3) (9-3) + (9-3)

  3. Calculation rules • Pattern matching f :: [Int] -> [Int] -> Int f [] ys = 0 f xs [] = 0 f (x:xs) (y:ys) = x+y f [1..3] [1..3] • f (1:[2..3]) [1..3] • f (1:[2..3]) (1:[2..3]) • 1+1 • 2

  4. Calculation rules (cont’d) • Guards f :: Int -> Int -> Int -> Int f m n p | m>=n && m>=p = m | n>=m && n>=p = n | otherwise = p f (2+3) (4-1) (3+9) ?? (2+3)>=(4-1) && (2+3)>=(3+9) ??  False && 3>=12 ??  5>=3 && 5>=(3+9) ??  False ??  True && 5>=(3+9) ?? otherwise ??  5>=(3+9) ??  True ??  5>=12  12 ??  False ?? 3>=5 && 3>=12

  5. Calculation rules (cont’d) • Local definitions f :: Int -> Int -> Int front (x:y:zs) = x+y f m n front [x] = x | notNil xs = front xs | otherwise = n notNil [] = False where xs = [m..n] notNil (_:_) = True f 3 5 ?? notNil xs  front xs ?? where where ?? xs = [3..5] xs = 3:[4..5] ??  3:[4..5]  3:4:[5] ??  notNil (3:[4..5])  3+4 ??  True  7

  6. Calculation rules (cont’d) Evaluation order = order in which applications are evaluated when there is a choice. • Evaluation is from the outside in: f1 e1 (f2 e2 17) • Evaluation is from left to right: f1 e1 + f2 e2

  7. List comprehension revisited List comprehension does not add any new programs to the Haskell language. It is a short-hand for a combination of multiple applications of map (concatMap) and filter. pyTriple n = [ (x,y,z) | x <- [2..n], y <- [x+1..n], z <- [y+1..n], x*x + y*y == z*z ] pyTriple’ n = concatMap (\x -> concatMap (\y -> concatMap (\z -> if x*x + y*y == z*z then [(x,y,z)] else []) [y+1..n]) [x+1..n]) [2..n] Evaluation rules: • [ e | False , q2, …, qk]  [] • [ e | v <- [], q2, …, qk]  [] • [ e | v <- a:xs, q2, …, qk]  [ e{a/v} | q2{a/v}, …, qk{a/v} ] ++ [ e | v <- xs, q2, …, qk ]

  8. Example [ x+y | x <- [1,2], isEven x, y <- [x..2*x] ] • [ 1+y | isEven 1, y <- [1..2*1] ] ++ [ x+y | x <- [2], isEven x, y <- [2..2*2] ]  [ 1+y | False, y <- [1..2*1] ] ++ [ x+y | x <- [2], isEven x, y <- [2..2*2] ]  [ 2+y | isEven 2, y <- [2..2*2] ]  [ 2+y | True, y <- [2..2*2] ]  [ 2+y | y <- [2..2*2] ] • [ 2+y | y <- 2:[3..2*2] ] • [ 2+2 | ] ++ [ 2+y | y <- [3..2*2] ] • [ 4 ] ++ [ 2+y | y <- [3..2*2] ] • [ 4 ] ++ [ 2+y | y <- 3:[4..2*2] ] • [ 4 ] ++ [ 2+3 | ] ++ [ 2+y | y <- [4..2*2] ] • [ 4 ] ++ [ 5 ] ++ [ 2+y | y <- [4..2*2] ] • [ 4,5 ] ++ [ 2+y | y <- [4..2*2] ] • [ 4,5 ] ++ [ 2+y | y <- 4:[] ] • [ 4,5 ] ++ [ 2+4 | ] ++ [ 2+y | y <- [] ] • [ 4,5 ] ++ [ 6 ] ++ [ 2+y | y <- [] ] • [ 4,5,6 ] ++ [ 2+y | y <- [] ] • [ 4,5,6 ] ++ [] • [ 4,5,6 ]

  9. Infinite lists ones :: [Int] ones = 1 : ones [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,………………^C{Interrupted!}] addFirstTwo :: [Int] -> Int addFirstTwo (x:y:zs) = x+y addFirstTwo ones • addFirstTwo (1:ones) • addFirstTwo (1:1:ones) • 1+1 • 2

  10. 2 3 4 5 6 7 8 9 10 11 12 13 2 3 5 7 9 11 13 2 3 5 7 11 13 Example: prime numbers primes :: [Integer] primes = sieve [2..] sieve :: [Integer] -> [Integer] sieve (x:xs) = x:sieve [ y | y <- xs, y `mod` x > 0] The sieve of Eratosthenes

  11. Example (cont’d) primes is an infinite list  be careful by using it 7 `elem` primes  True, but 6 `elem` primes does not terminate elemOrd :: Ord a => a -> [a] -> Bool elemOrd _ [] = False elemOrd y (x:xs) | x<y = elemOrd y xs | x==y = True | otherwise = False

  12. Example: Generating random numbers nextRand :: Int -> Int nextRand n = (multiplier * n + increment) `mod` modulus randomSequence :: Int -> [Int] randomSequence = iterate nextRand seed, multiplier, increment, modulus :: Int seed = 17489 multiplier = 25173 increment = 13849 modulus = 65536 randonSequence seed  [17489,59134,9327,52468,43805,8378,18395,…

  13. Another example pythagTriples = [ (x,y,z) | x <- [2..], y <- [x+1..], z <- [y+1..], x*x + y*y == z*z ] What is the result of pythagTriples? No output!!! pythagTriples' = [ (x,y,z) | z <- [4..], y <- [3..z-1], x <- [2..y-1], x*x + y*y == z*z ] or use a function infiniteProduct :: [a] -> [b] –> [(a,b)]

  14. Infinite elements in algebraic types data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show replaceRightLeaf :: Tree a -> Tree a -> Tree a replaceRightLeaf t1 Empty = t1 replaceRightLeaf t1 (Node x t2 t3) = Node x t2 (replaceRightLeaf t1 t3) loopRightTree :: Tree a -> Tree a loopRightTree t = replaceRightLeaf (loopRightTree t) t tree1 = Node 1 (Node 2 Empty Empty) (Node 3 Empty Empty) loopRightTree tree1  Node 1 (Node 2 Empty Empty) (Node 3 Empty (Node 1 (Node 2 Empty Empty) (Node 3 Empty (Node 1 (Node 2 Empty Empty) (Node 3 Empty …

More Related