Module
Control.Monad.Rec.Class
- Package
- purescript-tailrec
- Repository
- purescript/purescript-tailrec
#MonadRec Source
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 :: Number -> WriterT Sum (Eff (trace :: Trace)) Unit
loopWriter n = tailRecM go n
where
go 0 = do
lift $ trace "Done!"
pure (Done unit)
go n = do
tell $ Sum n
pure (Loop (n - 1))
Members
Instances
#tailRec Source
tailRec :: forall b a. (a -> Step a b) -> a -> b
Create a pure tail-recursive function of one argument
For example:
pow :: Number -> Number -> Number
pow n p = tailRec go { accum: 1, power: p }
where
go :: _ -> Step _ Number
go { accum: acc, power: 0 } = Done acc
go { accum: acc, power: p } = Loop { accum: acc * n, power: p - 1 }
- Modules
- Control.
Monad. Rec. Class