user1690181
user1690181

Reputation: 43

Couldn't match expected type Int with actual type Int -> Int in Haskell

The following code produces an error:

power:: Int -> Int -> Int
power a b 
        | a ==0 || b == 0      = 0
        | otherwise   = power ((multiply a a) (b-1))

multiply:: Int -> Int -> Int
multiply a b
        | a <= 0        = 0
        | otherwise     = (multiply (a-1) (b)) + b

The returned error is

power.hs:6:25:
    Couldn't match expected type `Int' with actual type `Int -> Int'
    In the return type of a call of `power'
    Probable cause: `power' is applied to too few arguments
    In the expression: power (multiply (a a) b - 1)
    In an equation for `power':
        power a b
          | b == 0 = 0
          | otherwise = power (multiply (a a) b - 1)

Upvotes: 2

Views: 4163

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185681

The error is in the expression power ((multiply a a) (b-1)). The problem is that extra pair of parentheses. You're actually only passing one argument to power, which is ((multiply a a) (b-1)). This expression is itself invalid, because the result of (multiply a a) is Int, which cannot accept arguments.

You should rewrite this as

| otherwise   = power (multiply a a) (b-1)

Upvotes: 4

Related Questions