1
accumulate :: (a -> a) -> a -> (a -> a) -> a -> (a -> a) -> a
accumulate combiner null_value term x next n = iter x null_value
where iter x result = if x > n
                        then result
                        else iter (next x) (combiner (term x) result)

This runs perfectly without the type signature, but with the type signature, I keep running into this error:

Couldn't match expected type `a' with actual type `a -> a'
  `a' is a rigid type variable bound by
      the type signature for
        accumulate :: (a -> a) -> a -> (a -> a) -> a -> (a -> a) -> a
      at haskell-sicp/chapter1.hs:131:15
In the expression: iter x null_value
In an equation for `accumulate':
    accumulate combiner null_value term x next n
      = iter x null_value
      where
          iter x result
            = if x > n then
                  result
              else
                  iter (next x) (combiner (term x) result)

I'm new to haskell, but I don't understand what is wrong with my type signature?

3
  • 2
    Try to see what is the type signature that the compiler is inferring automatically (using :t) and then try to understand how it fits there. Commented Jul 7, 2014 at 1:33
  • 3
    The first problem I see is that the type signature indicates that the function takes 5 arguments but the definition has 6 arguments. Also, since you are using > you will need an Ord constraint. Commented Jul 7, 2014 at 1:34
  • Thanks! That appears to have worked. Commented Jul 7, 2014 at 1:45

1 Answer 1

4

Near as I can tell, your combiner method takes two a's and returns one a, so it should be (a -> a -> a), a needs to be labeled an Ord, and you forgot to denote that the function returns an a. So, your actual type signature should be

accumulate :: Ord a => (a -> a -> a) -> a -> (a -> a) -> a -> (a -> a) -> a -> a
Sign up to request clarification or add additional context in comments.

Comments

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.