0

I am new to Haskell. I want to have a function in which I get an Int value from the user using

putStr "Please Enter Your Number : "
submit_ans<- getLine

then after a series of calculations returns another Int which will be used by another function that has called it. What is the type of the described function? should I use return function at the end?

thanks in advance!

Update #1

ok I updated the function as below:

myFunction ::  Int -> IO Int 
myFunction action = do

      putStr "\tPlease Enter Your Number : "            
      submit_ans <- getLine 

      if action == 1
        then do 
       myFunctionII read submit_ans  
        else do
       putStrLn("\n")

it gives me this error:

*** Term           : myFunction 
*** Type           : Int -> IO Int
*** Does not match : IO a
13
  • 1
    This "other function" you mention, what's its type? From your comments further down, it sounds like it's a pure function (i.e. non-IO). You can't perform an IO action from a pure function. Commented Jan 25, 2013 at 10:52
  • @gspr So that's the problem! I didn't know that I can't have IO actions in pure funtion! Then again, what's the point of IO Int if one cannot use the returned value? Commented Jan 25, 2013 at 11:06
  • Sure you can use it. But only from within the IO monad. Part of the "point" is indeed to dinstinguish between pure (non-IO) and impure (IO) code! A pure function always returns the same when given the same input. An impure one could burn down your house and eat your cat every third full moon on days beginning with s. It's nice to be able to treat the two kinds of functions differently. Commented Jan 25, 2013 at 11:09
  • 1
    So what I'm saying is: Write an IO-function that uses getLine to get the number, and then from that function call the pure functions that you want do stuff with the number you got (which you can get, since you're in the IO monad now). In other words: Have the IO code call the pure code, not the other way around. Commented Jan 25, 2013 at 11:11
  • I think I get the idea. I will try to implement it now. thanks! Commented Jan 25, 2013 at 11:14

3 Answers 3

2

Just write your function, say g, as a pure calculation, of type Int -> Int. Then you use it in IO monad as

...
putStr "Please Enter Your Number : "
submit_ans <- getLine
let i = g (read submit_ans)
...
print i

edit: Any Haskell value is pure. Functions are pure. IO x is a pure Haskell value. But it describes an impure computation, which will be run by the system (say, when your compiled application is run). As part of its execution, it will execute your pure function.

Sign up to request clarification or add additional context in comments.

Comments

2

putStrLn has type String -> IO () and it seems myFunctionII has type Int -> IO (), so the type of myFunction should be Int -> IO () since both branches of your if return an IO ():

myFunction :: Int -> IO ()
myFunction action = do
  putStr "\tPlease Enter Your Number : "
  submit_ans <- getLine

  if action == 1
     then myFunctionII (read submit_ans)
     else putStrLn "\n"

Comments

1

Since you are performing an IO operation and you are returning an Int so my best guess would be IO Int but I can't tell for sure because you question is too vague to answer clearly.

3 Comments

actually I did try myFunction :: IO Int but like I mentioned, another function calls this function and uses the returned Int value as an argument. it gave me error telling me IO Int is not Int.
Read about IO monad. You can't call an IO function inside a pure function (non IO types) cause that will break referential transparency. If you callee is also an IO function then you can do how you did for getLine.
I get your point but using IO Int and trying to call the pure function from within the function with IO actions did not work either. (I updated the post)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.