1

newcomer to Haskell here.

I have this function:

addNums num1 num2 = num1 + num2 

when I try to run addNums 1 -2 with GHCi, it returns this error:

<interactive>:40:1: error: * No instance for (Show (Integer -> Integer)) arising from a use of `print' (maybe you haven't applied a function to enough arguments?) * In a stmt of an interactive GHCi command: print it 

I notice that in order for it to run as intended, I need to add parentheses: addNums 1 (-2). Why is that?

1 Answer 1

3

The code you wrote is being parsed as (addNums 1) - 2, so the compiler takes the result of addNums 1, which is a function Integer -> Integer, and tries to subtract 2 from it.

For correct passing order, you need to add parentheses around the second parameter:

addNum 1 (-2) 
Sign up to request clarification or add additional context in comments.

7 Comments

That's interesting. Why doesn't it take in the second parameter if I have define the function to take in two parameters?
It does. Look at my final example. The function takes two parameters just fine.
I mean in the first paragraph, you said the code I wrote takes in (addNums 1) - 2.
Yes, that is how your code is parsed
Ok I believe I understand now. I was more so asking why exactly Haskell is parsing it like this. Is it because it see - as the infix operator which takes the highest precedence in this case?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.