Anonymous user
Loops/While: Difference between revisions
→{{header|Haskell}}: add monadic implementation
(Vedit macro language added; lang tag for VB.NET) |
(→{{header|Haskell}}: add monadic implementation) |
||
Line 197:
=={{header|Haskell}}==
<lang haskell>import Control.Monad (when)
main = loop 1024
import Control.Monad▼
loop (n `div` 2))▼
You could try to write a "while" that operates on monads:
whileM :: (Monad m) => m Bool -> m () -> m ()
whileM cond body = do c <- cond
when c (body >> whileM cond body)</lang>
You can use it like this
<lang haskell>import Data.IORef
main :: IO ()
main = do r <- newIORef 1024
whileM (do n <- readIORef r
(do n <- readIORef r
print n
modifyIORef r (`div` 2))</lang>
=={{header|Icon}}==
|