## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# How to do it...

1. Create a new project working-with-rank-n-type with a simple stack template:
`        stack new working-with-rank-n-type simple`
1. Open src/Main.hs. We will be adding our source here.
2. Add the language extension Rank2Types, and define Main module:
`        module Main where`
1. Import the ST monad module:
``` import Control.Monad.ST
import Data.STRef
1. Use the ST monad to calculate:
``` factorialST :: (Num t, Eq t) => t -> STRef s t -> ST s t
factorialST 0 x = readSTRef x
factorialST n x = do
x' <- readSTRef x
writeSTRef x \$! x' * n
factorialST (n-1) x```
1. Convert the preceding factorial function from ST s t to a pure function:
``` factorial n = runST \$ do
x <- newSTRef 1
factorialST n x```
1. Call the factorial in main:
` main :: ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required