Scrapbook-0.4.0: code examples
Copyright© Frank Jung 2025
LicenseGPL-3
Safe HaskellSafe-Inferred
LanguageHaskell2010

MySum

Description

This is a simple example of using the ST Monad for stateful summation of a list.

The ST monad in Haskell allows you to perform mutable (stateful) computations in a safe, local way. It stands for "State Thread" and is defined in Control.Monad.ST (ST).

Key Points

Local Mutability
You can create and modify mutable variables (STRefs) inside the ST monad.
Safety
Changes made in the ST monad are not visible outside; the mutation is encapsulated.
Pure Interface
When you run an ST computation (using runST), you get a pure result, and the mutable state cannot escape.

Example usage

Computes the sum of a list using the mySum function:

let total = mySum [1, 2, 3, 4, 5]
Synopsis

Documentation

mySum :: [Int] -> Int Source #

Computes the sum of a list of integers in a stateful manner.