3

This is very ad-hoc example describes Not in scope: isOne error that I have:

ignoreFirstOnes :: [Int] -> [Int]
ignoreFirstOnes (1:xs) = dropWhile isOne xs
ignoreFirstOnes xs     = xs
  where isOne = (== 1)

Strange that isOne function was defined in where, however compiler keeps complaining. I can rewrite that using guards or even to dropWhile (== 1) but I would like to understand how to make work the current example.

1
  • This is part of the reason I prefer case for most functions: I often want to use a where clause definition in multiple branches, and equational style doesn’t allow it. Commented May 14, 2016 at 19:27

1 Answer 1

5

Names defined in a where clause are only in scope over the branch that the where clause is attached to.

This version of your definition will compile, because I attached the where clause to the branch of ignoreFirstOnes that uses isOne.

ignoreFirstOnes :: [Int] -> [Int]
ignoreFirstOnes (1:xs) = dropWhile isOne xs
    where isOne = (== 1)
ignoreFirstOnes xs = xs

Though note that this definition is equivalent to ignoreFirstOnes = dropWhile (==1), which I think is simpler.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have already mentioned about dropWhile (==1) in question, meaning that this example is only to describe my concern. However, I have never known about this feature, so thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.