📘
HPM Education - Haskell
  • Introduction to Haskell
  • Introduction
    • Functions
    • Functional Programming vs Imperative Programming
    • Installing Haskell
    • Haskell Modules
    • Loading Modules into GHCi
    • Expressions
    • Laziness
    • Immutability
  • Types in Haskell
    • Introduction
    • Basic Types
    • Static Type Check
    • Polymorphic and Overloaded Types
    • Data Structure Types
      • Lists
        • List Functions
      • Tuples
    • Function Types
      • Curried Functions
      • Partial Application
  • Defining Functions / Working with Functions
    • The Layout Rule
    • Local Definitions
    • The Infix Operator
    • Conditionals
      • If-then-else Statements
      • MultiWayIf
      • Guarded Equations
      • Case-of Statements
    • Pattern Matching
      • Tuple Patterns
      • List Patterns
    • Lambda functions
    • Function Operators
  • List Comprehensions
    • List Comprehensions
  • Higher-order Functions
    • Introduction
    • The map Function
    • The filter Function
  • Recursion
    • Introduction
    • 4 Steps to Defining Recursive Functions
    • Recursion Practice
    • Folds
      • Fold Right (foldr)
      • Fold Left (foldl)
  • Cutom Types
    • Declaring Types
      • Type Synonyms
      • Data Declarations
      • Newtype declarations
  • Type Classes
    • Introduction
    • Basic Classes
      • Eq – Equality Types
      • Ord – ordered types
      • Show – Showable Types
      • Read – readable types
      • Num – Numeric Types
      • Integral – Integral Types
      • Fractional – Fractional Types
      • Enum – Enumeration Types
    • Derived Instances
    • Exercise – Making a Card Deck Type
  • Interactive Programming
    • Introduction
    • Input / Output Actions
    • Sequencing Actions
    • Exercise - Numbers Guessing Game
  • Functors, Applicatives and Monads
    • Introduction
    • Functors
    • Applicative Functors
    • Monads
      • Maybe Monad
      • List Monad
      • Monad Laws
  • References / Further Reading
Powered by GitBook
On this page

Was this helpful?

  1. Introduction

Laziness

We are starting to define our own functions now, so it is a good time to explain the concept of laziness in Haskell. Haskell is a lazy programming language, which means it does not evaluate expressions until really necessary. The opposite of lazy evaluation is strict evaluation, in which all expressions in a function call are evaluated before they are passed to the function.

Let's take a look at an example to better understand this:

ghci> f1 x y = x + 1

We see the function f1 takes in two arguments, but completely ignores its second argument `y` during evaluation. Let's see what happens when we actually pass some arguments to the function:

ghci> f1 1 (2^58)
2

Of course, the final result is 2, because 1 + 1 = 2, but what happened with our second argument (2 ^ 58)? It was never needed during function execution so it was actually never evaluated. From this example, we can see how lazy evaluation can save computational time by not doing unnecessary computations.

However, there is also a drawback to this strategy - our second parameter was not completely ignored, but instead stored in memory as an unevaluated expression (2 ^ 58). These unevaluated expressions can build up in heap memory and cause memory leakage. That is, our programs can have increased memory usage for no useful reason and if they consume all our system memory, the program will crash.

PreviousExpressionsNextImmutability

Last updated 2 years ago

Was this helpful?