> liftM (length) $ mapM Just [1,2,3,undefined]
Just 4
> liftM (take 3) $ mapM Just [1,2,3,undefined]
Just [1,2,3]
> liftM (take 3) $ mapM Just [1,2 ..]
_|_
This can be stripped down to the laziness behavior of sequence and from its
definition the strictness issues of (>>=).
Now the question arises, how to define sequence, to allow indefinite long
inputs?
Making it more complex did not help:
sequence xs = foldr my (return []) xs
where my = curry $ runKleisli $ Kleisli id *** Kleisli id >>^ uncurry (:)
> Now the question arises, how to define sequence, to allow indefinite long
> inputs?
If I understand you correctly, you would like the result Just [1,2,3]
instead of _|_ in the last example.
But that's unlikely to work. If you look at it in terms of sequence,
liftM (take 3) $ sequence [Just 42, Just 13, Just 7, ....]
the result depends on the whole list, not just the initial segment: If
there's a Nothing *anywhere* in the list, the result is Nothing, otherwise
it's Just [42,13,7]. So the result can only be determined after
inspecting *all* of the remaining list. If the list is infinite, this
cannot happen in finite time.
- Dirk
That was the point, I missed. And that is the reason why (bind) is strict in
both arguments for the Monad Maybe. Thank you.