Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COSC 4P41 – Functional 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 , • expression may describe infinite data structures, e.g, the infinite list of all prime numbers, • implications to program design. © M. Winter 8.1 COSC 4P41 – Functional Programming Example + h x y = x+x (9-3) h (9-3) (h 12 15) (9-3) + (9-3) 6+6 12 (9-3) + (9-3) © M. Winter 8.2 COSC 4P41 – Functional Programming Calculation rules • Pattern matching f f f f :: [Int] -> [Int] [] ys = xs [] = (x:xs) (y:ys) = -> Int 0 0 x+y f [1..3] [1..3] f (1:[2..3]) [1..3] f (1:[2..3]) (1:[2..3]) 1+1 2 © M. Winter 8.3 COSC 4P41 – Functional Programming 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) ?? 5>=3 && 5>=(3+9) ?? True && 5>=(3+9) ?? 5>=(3+9) ?? 5>=12 ?? False ?? 3>=5 && 3>=12 © M. Winter ?? ?? ?? ?? False && 3>=12 False otherwise True 12 8.4 COSC 4P41 – Functional Programming Calculation rules (cont’d) • Local definitions f :: Int -> Int -> Int f m n | notNil xs = front xs | otherwise = n where xs = [m..n] f 3 5 ?? notNil xs ?? where ?? xs = [3..5] ?? 3:[4..5] ?? notNil (3:[4..5]) ?? True © M. Winter front (x:y:zs) = x+y front [x] = x notNil [] = False notNil (_:_) = True front xs where xs = 3:[4..5] 3:4:[5] 3+4 7 8.5 COSC 4P41 – Functional Programming 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 © M. Winter 8.6 COSC 4P41 – Functional Programming 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 ] © M. Winter 8.7 COSC 4P41 – Functional Programming 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 ] © M. Winter 8.8 COSC 4P41 – Functional Programming 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 © M. Winter 8.9 COSC 4P41 – Functional Programming Example: prime numbers 2 3 4 5 6 7 2 3 5 7 2 3 5 7 8 9 10 9 11 12 13 11 13 11 13 The sieve of Eratosthenes primes :: [Integer] primes = sieve [2..] sieve :: [Integer] -> [Integer] sieve (x:xs) = x:sieve [ y | y <- xs, y `mod` x > 0] © M. Winter 8.10 COSC 4P41 – Functional Programming 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 © M. Winter 8.11 COSC 4P41 – Functional Programming 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,… © M. Winter 8.12 COSC 4P41 – Functional Programming 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)] © M. Winter 8.13 COSC 4P41 – Functional Programming 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 … © M. Winter 8.14