Haskell recurcive function type error with boolean

Viewed 51

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
1 Answers

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
Related