Reading two integers with getLine =

0

How do I read two numbers from an inputbox in Haskell online with getLine > > =

g::Float -> Float -> Float
g n t = 2 - (n*t**2+3))


main = getLine >>= (\n -> putStrLn(show (g (read n :: String -> Float))))
             g >>= (\t -> putStrLn(show (g (read t :: String -> Float))))
    
asked by Alejandro Caro 07.12.2017 в 21:44
source

1 answer

0

It is more convenient to use the block of than to write the operation based on the operator bind >>= as it seems you are trying to do.

Likewise, take advantage of the compiler's ability to choose the type of polymorphic operation as with those used to read or write in a console.

I think you're looking for something like this:

g :: Float -> Float -> Float
g n t = 2 - (n*t**2+3)

main :: IO ()
main =
  do
    x <- getLine
    y <- getLine
    print $ g (read x) (read y)

Both x and y are Strings . Since invoking the function g waits two Floats , the compiler knows that it has to use the function read of the instance of the class Read Float .

Note : I do not know what haskell online you are using, nor what is the inputbox .

Edited : if you want to show the result with a warning:

main :: IO ()
main =
  do
    x <- getLine
    y <- getLine
    putStr "El resultado es "
    print $ g (read x) (read y)

If that's not enough, you can convert the result to String with the function show

main :: IO ()
main =
  do
    x <- getLine
    y <- getLine
    let res = g (read x) (read y)
    putStrLn ("El resultado es " ++ show res)
    
answered by 09.12.2017 в 04:03