[Haskell-cafe] Just 3 >>= (1+)?

Vasyl Pasternak vasyl.pasternak at gmail.com
Sat May 9 16:37:43 EDT 2009


Hi,

Haskell expects the function with type (a -> m b) in the right side of
(>>=),
but you put there function with type (a -> a):

try:

:t (Just 3 >>=)
(Just 3 >>=) :: (Num a) => (a -> Maybe b) -> Maybe b

and:

:t (1+)
(1+) :: (Num a) => a -> a

You should put (1+) into Maybe monad, just do return.(1+), so

Just 3 >>= return . (1+)

will return `Just 4`

-- 
Best regards,
Vasyl Pasternak

2009/5/9 michael rice <nowgate at yahoo.com>

> Why doesn't this work?
>
> Michael
>
> ================
>
> data Maybe a = Nothing | Just a
>
> instance Monad Maybe where
>     return         = Just
>     fail           = Nothing
>     Nothing  >>= f = Nothing
>     (Just x) >>= f = f x
>
> instance MonadPlus Maybe where
>     mzero             = Nothing
>     Nothing `mplus` x = x
>     x `mplus` _       = x
>
> ================
>
> [michael at localhost ~]$ ghci
> GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> Prelude> Just 3 >>= (1+)
>
> <interactive>:1:0:
>     No instance for (Num (Maybe b))
>       arising from a use of `it' at <interactive>:1:0-14
>     Possible fix: add an instance declaration for (Num (Maybe b))
>     In the first argument of `print', namely `it'
>     In a stmt of a 'do' expression: print it
> Prelude>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090509/2cee2ab1/attachment.htm


More information about the Haskell-Cafe mailing list