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 }
- Modules
- Control.
Monad. Rec. Class