2 ms·
Monads don't (quite) short-circuit the later computations unlike a return statement. Anyway, I'd like to see an example of writing this using monads, since it
by rwmj 12y ago
Monads don't (quite) short-circuit the later computations unlike a return statement. Anyway, I'd like to see an example of writing this using monads, since it makes the idea more concrete.
- tel 12y agoI can write a continuation monad in Haskell which would. Something like {-# LANGUAGE DeriveFunctor #-} module Return where import Control.Monad newtype Cont r a = Cont { runCont :: (a -> r) -> r } deriving Functor instance Monad (Cont r) where return a = Cont (\k -> k a) m >>= f = Cont (\k -> runCont m (\a -> runCont (f a) k)) early :: r -> Cont r a early a = Cont (\_ -> a) ex :: Cont Integer [Integer] ex = do forM [1..] $ \i -> do when (i == 33) (early i) return i