130 likes | 302 Views
Laziness of Haskell. One important thing to know about Haskell is that it performs lazy evaluation of expressions. Here, “lazy” means that expressions are only evaluated when absolutely necessary for the current computation.
E N D
Laziness of Haskell • One important thing to know about Haskell is that it performs lazy evaluation of expressions. • Here, “lazy” means that expressions are only evaluated when absolutely necessary for the current computation. • For example, this allows us to work with infinite lists without getting stuck in an infinite computation: • >take 10 [1..] • [1,2,3,4,5,6,7,8,9,10] Introduction to Artificial Intelligence Data Structures in Haskell
Example for Expression Evaluation (1) • mymap f [] = [] • mymap f (x:xs) = (f x):(mymap f xs) • mymapisPrime [1, 2, 3] • (isPrime 1):(mymapisPrime [2, 3]) • False:(mymapisPrime [2, 3]) • False:(isPrime 2):(mymapisPrime [3]) • False:True:(mymapisPrime [3]) • False:True:(isPrime 3):(mymapisPrime[]) • False:True:True:[] • Which is the same as: • [False,True,True] Introduction to Artificial Intelligence Data Structures in Haskell
Example for Expression Evaluation (2) • foldl f z [] = z • foldl f z (x:xs) = foldl f (f z x) xs • foldl (+) 0 [1, 2, 3] • foldl (+) ((+) 0 1) [2, 3] • foldl (+) 1 [2, 3] • foldl (+) ((+) 1 2) [3] • foldl(+) 3 [3] • foldl (+) ((+) 3 3) [] • foldl (+) 6 [] • 6 Introduction to Artificial Intelligence Data Structures in Haskell
Haskell Code Written in Class (1) • isEven n = if mod n 2 == 0 then "yes!" • else "no!" • sign x • | x < 0 = -1 • | x > 0 = 1 • | otherwise = 0 • data Shape = Circle Float | Rectangle Float Float • area (Circle r) = pi*r*r • area (Rectangle x y) = x*y Introduction to Artificial Intelligence Data Structures in Haskell
Haskell Code Written in Class (2) • data BinaryTree a = • Leaf a | Branch (BinaryTree a) a (BinaryTree a) • mytree = Branch (Branch (Leaf 2) 7 (Leaf 5)) 3 (Branch (Branch (Leaf 1) 3 (Leaf 4)) 8 (Leaf 9)) 3 8 7 2 5 3 9 1 4 Introduction to Artificial Intelligence Data Structures in Haskell
Haskell Code Written in Class (3) • nodeCount (Leaf x) = 1 • nodeCount (Branch left x right) = 1 + nodeCount left + nodeCount right • treeHeight (Leaf x) = 0 • treeHeight (Branch left x right) = 1 + max (treeHeight left) (treeHeight right) • tree2list (Leaf x) = [x] • tree2list (Branch left x right) = (tree2list left) ++ [x] ++ (tree2list right) • foldTree f z (Leaf x) = f x z • foldTree f z (Branch left x right) = foldTreef (f x (foldTree f z right)) left Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (1) • *Main> zip [1..10] ['a'..'z'] • [(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),(6,'f'),(7,'g'),(8,'h'),(9,'i'),(10,'j')] • it :: [(Integer, Char)] • *Main> zip [1..10] ['a'..] • [(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),(6,'f'),(7,'g'),(8,'h'),(9,'i'),(10,'j')] • It :: [(Integer, Char)]*Main> :t foldl • foldl :: (a -> b -> a) -> a -> [b] -> a • *Main> foldl (*) 1 [1..6] • 720 • it :: Integer Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (2) • *Main> isEven 5 • "no!" • it :: [Char] • *Main> isEven 6 • "yes!" • it :: [Char] • *Main> :t isEven • isEven :: Integral a => a -> [Char] Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (3) • *Main> sign (-9) • -1 • it :: Integer • *Main> sign 8 • 1 • it :: Integer • *Main> sign 0 • 0 • it :: Integer Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (4) • *Main> let x = Circle 9 • x :: Shape • *Main> let y = Rectangle 9 • y :: Float -> Shape • *Main> let y = Rectangle 9 8 • y :: Shape • *Main> area x • 254.46901 • it :: Float • *Main> area y • 72.0 • It :: Float • *Main> :t areaarea:: Shape -> Float Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (5) • *Main> nodeCountmytree • 9 • it :: Integer • *Main>treeHeightmytree • 3 • it :: Integer • *Main> tree2list mytree • [2,7,5,3,1,3,4,8,9] • it :: [Integer] Introduction to Artificial Intelligence Data Structures in Haskell
Our Haskell Session Protocol (6) • *Main> foldTree (:) [] mytree • [2,7,5,3,1,3,4,8,9] • it :: [Integer] • *Main>foldTree (+) 0 mytree • 42 • it :: Integer Introduction to Artificial Intelligence Data Structures in Haskell