siddk
siddk

Reputation: 13

Expected type error in Haskell code

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?

Upvotes: 1

Views: 89

Answers (1)

DrJPepper
DrJPepper

Reputation: 168

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

Upvotes: 4

Related Questions