White_Sirilo
White_Sirilo

Reputation: 284

Type error in explicitly typed binding - Haskell

I've been struggling for like an hour to understand some things in higher order functions and now I am at the point that I cannot move any further because of this error:

    hof :: [Integer] -> (Integer -> Integer)
    isIn :: [Integer] -> Integer -> Integer
    isIn [] s = 0
    isIn [] _ = 0
    isIn (h:t) s
        | h == s = 0 {-<-------- error points here here-}
        | otherwise = isIn(t) + 1
    hof s = \n -> isIn s n

      ERROR file:.\Lab2.hs:111 - Type error in explicitly typed binding
      *** Term           : isIn
      *** Type           : [Integer] -> Integer -> Integer -> Integer
      *** Does not match : [Integer] -> Integer -> Integer

Upvotes: 1

Views: 104

Answers (1)

chepner
chepner

Reputation: 531490

You are missing the second argument in the recursive call to isIn; it should be

 | otherwise = isIn t s + 1
 --                   ^

While not an error, you also have a redundant base case; s and _ are both irrefutable patterns, so just isIn [] _ = 0 is sufficient.

Also not an error, but there is no difference between hof and isIn, as you can eta-reduce the definition to

hof s = isIn s

and again to

hof = isIn

Upvotes: 6

Related Questions