Module

# Control.Monad.Rec.Class

- Package
- purescript-tailrec
- Repository
- purescript/purescript-tailrec

### #MonadRec Source

`class MonadRec :: (Type -> Type) -> Constraint`

`class (Monad m) <= MonadRec m where`

This type class captures those monads which support tail recursion in constant stack space.

The `tailRecM`

function takes a step function, and applies that step
function recursively until a pure value of type `b`

is found.

Instances are provided for standard monad transformers.

For example:

```
loopWriter :: Int -> WriterT (Additive Int) Effect Unit
loopWriter n = tailRecM go n
where
go 0 = do
traceM "Done!"
pure (Done unit)
go i = do
tell $ Additive i
pure (Loop (i - 1))
```

#### Members

#### Instances

### #tailRec Source

`tailRec :: forall a b. (a -> Step a b) -> a -> b`

Create a pure tail-recursive function of one argument

For example:

```
pow :: Int -> Int -> Int
pow n p = tailRec go { accum: 1, power: p }
where
go :: _ -> Step _ Int
go { accum: acc, power: 0 } = Done acc
go { accum: acc, power: p } = Loop { accum: acc * n, power: p - 1 }
```

### #tailRecM3 Source

`tailRecM3 :: forall m a b c d. MonadRec m => (a -> b -> c -> m (Step { a :: a, b :: b, c :: c } d)) -> a -> b -> c -> m d`

Create a tail-recursive function of three arguments which uses constant stack space.

The `loop3`

helper function provides a curried alternative to the `Loop`

constructor for this function.

- Modules
- Control.
Monad. Rec. Class