6

I want to write a Haskell program that calculates the sum of numbers between 2 given numbers. I have the following code:

sumInt :: Int -> Int -> Int
sumInt x y
   | x > y = 0
   | otherwise = x + sumInt x+1 y

But when I compile it I get the following error:

SumInt is applied to too few arguments.

I don't understand what I'm doing wrong. Any ideas?

1
  • Why was this downvoted? +1 even just to negate, this question is definitely within the scope of SO (and it's a good one too). Commented Nov 26, 2014 at 18:03

1 Answer 1

7

You need parentheses around x+1:

| otherwise = x + sumInt (x + 1) y

The reason is that function application binds more tightly than operators, so whenever you see

f x <> y

This is always parsed as

(f x) <> y

and never as

f (x <> y)
Sign up to request clarification or add additional context in comments.

2 Comments

How did the compiler try to read it when it reported the error? can you demonstrate using parentheses?
@MasterMastic The compiler would have read this particular expression as x + (sumInt x) + (1 y). Due to polymorphism of numeric literals, 1 y actually typechecks, it just doesn't have an instance of Num available that is a function. The error came from sumInt x, which is said to have type Int, with x being known as Int, so the first error it encounters is a mismatch between Int -> Int and Int, for which it suggests you don't have enough parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.