# Laziness in Haskell Haskell is a functional language based on typed lambda calculus where everything is a well-typed expression, meaning it would allow you to do the following: ```haskell apply :: (Int -> Int) -> Int apply f = f (apply f) ``` In most other languages, trying to evaluate this function on any argument would lead to stack overflow. However, the following safely terminates in Haskell (as it should): ```haskell value = apply (\_ -> 5) -- apply (\_ -> 5) = (\_ -> 5) (apply (\_ -> 5)) = 5 ``` This is possible thanks to the special runtime system of Haskell, or RTS for short. It computes values lazily rather than eagerly; this means that Rust: nom ```haskell ignore foldr :: (a -> b -> b) -> b -> [a] -> b foldr f b [] = b foldr f b (x : xs) = f x (foldr f b xs) -- foldr (:) [] [1..1000] = 1 : foldr (:) [] [2..1000] = -- 1 : 2 : foldr (:) [] [3..1000] foldl :: (b -> a -> b) -> b -> [a] -> b foldl _ b [] = b foldl f b (x : xs) = foldl f (f b x) xs -- foldl (;) [] [1..1000] = foldl (;) (1 : []) [2..1000] -- = foldl (;) (2 : 1 : []) [3..1000] = ... ``` ```haskell foldl' :: (b -> a -> b) -> b -> [a] -> b foldl' _ b [] = b foldl' f b (x : xs) = let b' = f b x in b' `seq` foldl' f b' xs -- ($!) foldl'' :: (b -> a -> b) -> b -> [a] -> b foldl'' _ !b [] = b foldl'' f !b (x : xs) = foldl'' f (f b x) xs ``` Strict fields in records ```haskell data Point = Point { x :: !Integer, y :: ![Integer] } ```