Can type be changed along a >>= chain?

The first of these works, but not the second. It would seem that the type cannot change along a >>= chain, but I may be missing something in the code. Is the second example illegal? If so, is there a different way to change a String to an Int along the >>= chain? Michael =========== import Data.Char {- transform :: IO () transform = putStrLn "What is your word?" >> getLine >>= \str -> return ('Q':str) >>= \str -> return ('Z':str) >>= \str -> putStrLn $ "Transformed word is " ++ show str -} 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

On Mon, Oct 12, 2009 at 6:37 PM, michael rice
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? Cheers, /Niklas

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.
Thanks!
Michael
--- On Mon, 10/12/09, Niklas Broberg

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.
Likewise,
ghci> :t (+)
(+) :: (Num a) => a -> a -> a
fromIntegral ((1 :: Int) + 2) + (3 :: Integer)
6
This shows clearly that the types are not the same along the "+ chain".
2009/10/12 michael rice
Dumb! I just figured out I was entering the input string in quotes.
= chain. I was having trouble doing it in a different problem, created
So, I suppose the answer to my question is yes, type CAN be changed along a this small example to illustrate the problem, and then screwed it up putting quotes around my input string.
Thanks!
Michael
--- On *Mon, 10/12/09, Niklas Broberg
* wrote: From: Niklas Broberg
Subject: Re: [Haskell-cafe] Can type be changed along a >>= chain? To: "michael rice" Cc: haskell-cafe@haskell.org Date: Monday, October 12, 2009, 12:43 PM On Mon, Oct 12, 2009 at 6:37 PM, michael rice
http://mc/compose?to=nowgate@yahoo.com 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? Cheers, /Niklas
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

minh thu
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.
(But note that the monad 'm' has to be the same all the way. You can't switch from, say, IO to ST in the middle of the computation.)
Likewise,
Contrariwise, I'd say.
ghci> :t (+) (+) :: (Num a) => a -> a -> a
I.e. (+) requires the same type on both sides...
fromIntegral ((1 :: Int) + 2) + (3 :: Integer) 6
...but 'fromIntegral' converts it. Did I miss some subtle point here? -k -- If I haven't seen further, it is by standing in the footprints of giants

2009/10/12 Ketil Malde
minh thu
writes: 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.
(But note that the monad 'm' has to be the same all the way. You can't switch from, say, IO to ST in the middle of the computation.)
Likewise,
Contrariwise, I'd say.
ghci> :t (+) (+) :: (Num a) => a -> a -> a
I.e. (+) requires the same type on both sides...
fromIntegral ((1 :: Int) + 2) + (3 :: Integer) 6
...but 'fromIntegral' converts it. Did I miss some subtle point here?
I was talking about the types of the two + applications. They have a different types, just like the >>= in the parent's message can have different types. My fromIntegral has a role similar to the read s :: Int of the original question. Cheers, Thu

2009/10/12 michael rice
The first of these works, but not the second. It would seem that the type cannot change along a >>= chain, but I may be missing something in the code.
Is the second example illegal? If so, is there a different way to change a String to an Int along the >>= chain?
Michael
===========
import Data.Char
{- transform :: IO () transform = putStrLn "What is your word?" >> getLine >>= \str -> return ('Q':str) >>= \str -> return ('Z':str) >>= \str -> putStrLn $ "Transformed word is " ++ show str -}
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
Both seem good to me and my old ghci (6.6.1)... Thu
participants (4)
-
Ketil Malde
-
michael rice
-
minh thu
-
Niklas Broberg