Asaf
Asaf

Reputation: 8216

Trying to make a simple math equation in Haskell

I'm trying to make a simple function that gets 2 variables from the user (x,y)
makes a calculation, and prints it out.
for some reason without success:

main = do
    putStrLn "Insert Number1"
    x <- readLn
    putStrLn "Insert Number2"
    y <- readLn
    z = (x * y * 0.01)
    putStrLn "Result: " ++z

The Error I get:

test.hs:6:11: parse error on input `='

Upvotes: 1

Views: 534

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152957

Use let to bind new variables. You also have a few errors on the final line: first, you must explicitly convert between Double and String (using, for example, show), and secondly, you need to remember precedence. In Haskell, function application binds tighter than anything except record updates, so what you wrote parses as (putStrLn "Result: ") ++ z, which doesn't really make sense. With these things fixed:

main = do
    putStrLn "Insert Number1"
    x <- readLn
    putStrLn "Insert Number2"
    y <- readLn
    let z = x * y * 0.01
    putStrLn ("Result: " ++ show z)

Upvotes: 9

Related Questions