Read 2 values from keyboard in Haskell online

0

I am using a Haskell compiler online and I would like to be able to read two values from the keyboard (from the input box) two numbers but it throws an error and I do not know how to correct it.

g::Float -> Float -> Float
g n u   = 2 - (7/(5**n*u))

main :: IO ()
main = do 
        n <- getLine
        u <- getLine
        putStrLn (show (g (read n u :: (Float->Float->Float))))
    
asked by Alejandro Caro 03.12.2017 в 22:18
source

2 answers

0

The read function has the following signature:

read :: Read a => String -> a

You are trying to call it with two arguments of type String ( n and u ) instead of one only; you have to apply the read function separately to each data.

On the other hand, the type annotation that you provide ( :: (Float->Float->Float) ) is that of the function g , but once the arguments are passed to it, the type is simply Float . It is not necessary to specify the type for the results of read , since Haskell can infer it by the type of the function g .

g :: Float -> Float -> Float
g n u = 2 - (7/(5**n*u))

main :: IO ()
main = do 
    n <- getLine
    u <- getLine
    putStrLn (show (g (read n) (read u)))
    
answered by 04.12.2017 / 12:17
source
0

I was able to get the exercise by also defining the data type in the values of the entry

g::Float -> Float -> Float
g n u = 2 - (7/(5**n*u))

main :: IO ()
main = do 
        n <- getLine
        u <- getLine
        putStrLn (show (g (read n :: Float)(read u :: Float)))
    
answered by 04.12.2017 в 14:33