Rahul Agarwal
Rahul Agarwal

Reputation: 23

Haskell recurcive function type error with boolean

I'm new to haskell. Don't want to use any library function, learning the language.

I'm trying to write a recursive function to find if a number is a perfect square. But the function is throwing type error. Don't know what am I doing wrong.

perfectSq' n y = if (n*n) == y then True else if n == 0 then False else perfectSq' n-1 y

• Couldn't match type ‘t -> Bool’ with ‘Bool’
      Expected type: t -> Bool
        Actual type: t -> t -> Bool
• Relevant bindings include
        perfectSq' :: t -> Bool

Upvotes: 1

Views: 69

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80880

The expression perfectSq' n-1 y is compiled as (perfectSq' n) - (1 y) - that is, function application binds stronger than the minus operator.

You need to add parentheses to help the compiler with correct order of operations:

perfectSq' (n-1) y

Upvotes: 4

Related Questions