[Haskell-beginners] Problem combining monads <sigh!>
Karol Samborski
edv.karol at gmail.com
Mon Apr 7 09:13:59 UTC 2014
Hi John!
2014-04-07 11:06 GMT+02:00 John M. Dlugosz <ngnr63q02 at sneakemail.com>:
> I think I was looking at the page
> <http://learnyouahaskell.com/for-a-few-monads-more#state> a few paragraphs
> under the H3 “The join function”. It mentions “…we wanted to combine
> several of them into one, be it with <*> or >>=,…”
>
> Now I'm actually quite comfortable with Applicative syntax, and am
> preferring it in places where older texts and examples use monads or fmap
> etc. (I look forward to an updated standard where Monads are derived from
> Applicatives.)
>
> (Aside: is there a "history" function in GHCi to show the last few
> commands I typed?)
>
> So, I tried a GHCi example starting with:
>
> λ>(+7) <$> [1,2,3]
>
> λ>[(+7), (*2)] <*> [1,2,3]
>
> no sweat. Now use >>= to do the same thing:
>
> λ>[(+7), (*2)] >>= [1,2,3]
> -- nope
> (I'll spare the error messages, which inspired some of the other things to
> try, some of which were just to explore the errors themselves)
>
> λ>[(+7), (*2)] <<= [1,2,3]
> -- nope
>
> λ>[(+7), (*2)] =<< [1,2,3]
> -- nope
>
> λ>[return (+7), return(*2)] =<< [1,2,3]
> -- nope
>
> (Use a single function as a simpler starting point)
>
> λ>(+7) =<< [1,2,3]
> -- nope
>
> λ>return (+7) =<< [1,2,3]
> -- nope
>
> λ>return (+7) >>= [1,2,3]
> -- nope
>
> λ>return [1,2,3]
> [1,2,3]
> it :: [Integer]
> -- just checking
>
> λ>[1,2,3] >>= (+7)
> -- nope
> λ>[1,2,3] >>= do (+7)
> -- nope. Error messages are talking about
> No instance for (Num [b0]) arising from the literal `1'
> so why is it looking inside the monad at one of the values and thinking that
> itself should be a list?
>
> λ>[1,2,3] >>= [(+7)]
> -- nope
>
> λ>[1,2,3] >>= (+7).return
> -- nope, but got *two* errors out of that one
You're close. It should be:
[1,2,3] >>= return . (+7)
Best,
Karol
More information about the Beginners
mailing list