0

In my program I need to make a decision based on user input. Let's say the valid inputs are one, two, three. I first need to convert such an input to an Int because I need to compare it to other data in my program. Which is why I have a function toInt :: String -> Int which turns a String into an Int (one = 1, two = 2, three = 3). As of now I try to use getLine to retrieve the user input however this is of type IO String so I can't use my function with this input.

Thus my questions are: should I use getLine in this case? And if so how can I modify the user input or toInt so that it will work? And if not what else can I do so that I can get toInt to work with the user input?

Joe
  • 3
  • 2
  • For one, `fmap :: Functor f => (a -> b) -> (f a -> f b)` could be specialized into `fmap :: (String -> Int) -> (IO String -> IO Int)`, so you'd have `fmap toInt :: IO String -> IO Int` - which would get you a way to obtain an IO action giving `Int`. You would better follow detailed explanation and exercise for IO, though. – Abastro Aug 12 '22 at 01:00
  • 1
    Hello! This is a basic question on how input and output work in Haskell. Have you tried to read up any material explaining the topic? For example: https://wiki.haskell.org/Introduction_to_IO – Artem Pelenitsyn Aug 12 '22 at 01:14

0 Answers0