[Haskell-beginners] is this right?

Sylvain Henry sylvain at haskus.fr
Thu Sep 22 10:33:11 UTC 2016


Hi,

Yes it's correct. You can check this with ghci:

f :: Integer -> Maybe Integer
f 5 = Nothing
f x = Just x

 > let xs = [1..] :: [Integer]
 > mapM f xs
Nothing  (it doesn't loop forever)
 > :sprint xs
xs = 1 : 2 : 3 : 4 : 5 : _   (the tail after 5 is not evaluated)

Cheers
Sylvain

On 22/09/2016 11:57, Dennis Raddle wrote:
> I want to evaluate a function on a series of inputs:
>
> f :: a -> Maybe b
>
> then collect the results [b] if they are all Just, or terminate the 
> computation immediately upon hitting Nothing.
>
> This is exactly what mapM does in the Maybe monad, correct?
>
> In particular I want to make sure that it will not try to evaluate 
> anything past the first 'Nothing' result as the efficiency of my 
> design is based on that.
>
> D
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160922/bd17a99a/attachment.html>


More information about the Beginners mailing list