having trouble with helloworld style program

Hello everybody, All I want to do in the following code is to read an integer from stdin and print it back to stdout. Can somebody help me fix my code snippet below.. Thanks, Sunil module Main where import Prelude stringToInt::String->Int stringToInt str = read str main = do x<-getLine y<-stringToInt x print y

On Mon, Aug 8, 2011 at 2:39 PM, Sunil S Nandihalli
Hello everybody, All I want to do in the following code is to read an integer from stdin and print it back to stdout. Can somebody help me fix my code snippet below.. Thanks, Sunil
module Main where import Prelude stringToInt::String->Int stringToInt str = read str main = do x<-getLine y<-stringToInt x print y
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
The code is *almost* right. You just need to change the second to last line to be: let y = stringToInt x Hoping someone else steps in with a good, beginner-friendly explanation of *why*. Michael

Sunil S Nandihalli
module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main = do x<-getLine y<-stringToInt x print y
It is a type problem. Using the '<-' syntax for 'stringToInt' requires that it is a monadic function (like: String -> IO Int), but it is not. Since it is not monadic, you can simply let-bind the result to a name: do xStr <- getLine let x = stringToInt xStr print x Or more simply: do xStr <- getLine print (stringToInt xStr) Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

here...
<- is good for "pulling out" a result/value out of an IO Action (after
running it)
let is good for the assignment of a pure functional evaluation/result
Hope this is roughly correct :-)
Hartmut
On Mon, Aug 8, 2011 at 1:45 PM, Ertugrul Soeylemez
Sunil S Nandihalli
wrote: module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main = do x<-getLine y<-stringToInt x print y
It is a type problem. Using the '<-' syntax for 'stringToInt' requires that it is a monadic function (like: String -> IO Int), but it is not. Since it is not monadic, you can simply let-bind the result to a name:
do xStr <- getLine let x = stringToInt xStr print x
Or more simply:
do xStr <- getLine print (stringToInt xStr)
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

thanks Micheal and Ertugrul!
Sunil
On Mon, Aug 8, 2011 at 5:15 PM, Ertugrul Soeylemez
Sunil S Nandihalli
wrote: module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main = do x<-getLine y<-stringToInt x print y
It is a type problem. Using the '<-' syntax for 'stringToInt' requires that it is a monadic function (like: String -> IO Int), but it is not. Since it is not monadic, you can simply let-bind the result to a name:
do xStr <- getLine let x = stringToInt xStr print x
Or more simply:
do xStr <- getLine print (stringToInt xStr)
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Dear Sunil I am very new to haskell, started reading http://learnyouahaskell.com and completed upto functionally-solving-problems. In your example, it is very clear where the issue is Please check below code and try to infer what is wrong with you. Please let me know if you want explanation from my side. module Main where import Prelude stringToInt::String->Int stringToInt str = read str main =do x<-getLine let y=stringToInt x print y On Mon, Aug 8, 2011 at 5:09 PM, Sunil S Nandihalli < sunil.nandihalli@gmail.com> wrote:
Hello everybody, All I want to do in the following code is to read an integer from stdin and print it back to stdout. Can somebody help me fix my code snippet below..
Thanks, Sunil
module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main = do x<-getLine y<-stringToInt x print y
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Ohh, I missed to see reply from others. Anyway I am putting one more method
just from my side
module Main where
import Prelude
stringToInt::String->Int
stringToInt str = read str
main =do
x<-getLine
y<-return x
print $ stringToInt y
Regards
Sukumaran
On Mon, Aug 8, 2011 at 5:33 PM, Sukumaran A
Dear Sunil I am very new to haskell, started reading http://learnyouahaskell.com and completed upto functionally-solving-problems. In your example, it is very clear where the issue is
Please check below code and try to infer what is wrong with you. Please let me know if you want explanation from my side.
module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main =do x<-getLine let y=stringToInt x print y
On Mon, Aug 8, 2011 at 5:09 PM, Sunil S Nandihalli < sunil.nandihalli@gmail.com> wrote:
Hello everybody, All I want to do in the following code is to read an integer from stdin and print it back to stdout. Can somebody help me fix my code snippet below..
Thanks, Sunil
module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main = do x<-getLine y<-stringToInt x print y
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Sukumaran A
Ohh, I missed to see reply from others. Anyway I am putting one more method just from my side
module Main where import Prelude
stringToInt::String->Int stringToInt str = read str
main =do x<-getLine y<-return x print $ stringToInt y
This is exactly equivalent to the other methods, but has the additional 'return', which is a no-op. Please do not suggest such a style. To be honest, I think the best coding style for this particular example is not to use do-notation at all: module Main where main :: IO () main = let stringToInt :: String -> Int stringToInt = read in fmap stringToInt getLine >>= print Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

main =do x<-getLine y<-return x print $ stringToInt y
This is exactly equivalent to the other methods, but has the additional 'return', which is a no-op. Please do not suggest such a style.
Really? Without the return it will not work. So, it isn't a noop. Stylistically, I like it better than the 'let' in 'do' notation.

David Place
main =do x<-getLine y<-return x print $ stringToInt y
This is exactly equivalent to the other methods, but has the additional 'return', which is a no-op. Please do not suggest such a style.
Really? Without the return it will not work. So, it isn't a noop. Stylistically, I like it better than the 'let' in 'do' notation.
It will not work, because you have additional identifiers. The code y <- return x really just states the same as: let y = x and this is a requirement of the monad laws: c >=> return = c return >=> c = c Abusing 'return' is mostly an artifact from former imperative programmers, who think that 'return' in Haskell is a control construct. It's not; it's just a function. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

On Mon, Aug 8, 2011 at 11:08 AM, David Place
main =do
x<-getLine
y<-return x
print $ stringToInt y
This is exactly equivalent to the other methods, but has the additional 'return', which is a no-op. Please do not suggest such a style.
Really? Without the return it will not work. So, it isn't a noop. Stylistically, I like it better than the 'let' in 'do' notation.
I think he means that the entire line is a no-op. The line:
y <- return x
is equivalent to:
let y = x
and since re-naming a value doesn't perform any computation, you may as well delete the line and use 'x' everywhere you would have used 'y':
main =do x<-getLine print $ stringToInt x
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (7)
-
Antoine Latter
-
David Place
-
Ertugrul Soeylemez
-
Hartmut
-
Michael Snoyman
-
Sukumaran A
-
Sunil S Nandihalli