[Haskell-cafe] Can type be changed along a >>= chain?
noteed at gmail.com
Mon Oct 12 13:05:48 EDT 2009
I hope you're not building some unneeded "rules" in your head. There is no
reason to believe there is something to be remembered about whether or not
"types can change along a >>= chain". That chain has nothing special in
Haskell. >>= is just an operator, much like ++, ! or .
ghci> :t (>>=)
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
This says that, you provide an a and you get a b. Nothing says the a and b
have to be the same upon successive uses.
ghci> :t (+)
(+) :: (Num a) => a -> a -> a
fromIntegral ((1 :: Int) + 2) + (3 :: Integer)
This shows clearly that the types are not the same along the "+ chain".
2009/10/12 michael rice <nowgate at yahoo.com>
> Dumb! I just figured out I was entering the input string in quotes.
> So, I suppose the answer to my question is yes, type CAN be changed along a
> >>= chain. I was having trouble doing it in a different problem, created
> this small example to illustrate the problem, and then screwed it up putting
> quotes around my input string.
> --- On *Mon, 10/12/09, Niklas Broberg <niklas.broberg at gmail.com>* wrote:
> From: Niklas Broberg <niklas.broberg at gmail.com>
> Subject: Re: [Haskell-cafe] Can type be changed along a >>= chain?
> To: "michael rice" <nowgate at yahoo.com>
> Cc: haskell-cafe at haskell.org
> Date: Monday, October 12, 2009, 12:43 PM
> On Mon, Oct 12, 2009 at 6:37 PM, michael rice <nowgate at yahoo.com<http://firstname.lastname@example.org>
> > wrote:
>> transform :: IO ()
>> transform = putStrLn "What is your digit string?"
>> >> getLine
>> >>= \str -> return ('9':str)
>> >>= \str -> return (read str :: Int)
>> >>= \i -> putStrLn $ "The number is " ++ show i
> This code works perfectly for me. What problem are you seeing specifically?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe