I am trying to use isNothing in my code in Haskell, and it is giving me the error
<interactive>:97:23: error:
• Variable not in scope: isNothing :: Maybe t -> Bool
• Perhaps you meant data constructor ‘Nothing’ (imported from Prelude)
The code line I have is as follows -
maybeMap f value = if isNothing (value) then value else Just (f (check value))
this works fine if I replace isNothing value
with value == Nothing
, so I am confused why the previous is not working.
CodePudding user response:
The function isNothing
is not part of the standard prelude. Rather, it's distributed as part of the Data.Maybe
module. To use isNothing
, you'll need to explicitly import that module:
import Data.Maybe
CodePudding user response:
First thing's first, the key phrase in the error message is:
Variable not in scope: isNothing
Which means that the compiler just isn't aware of anything named isNothing
.
That immediately tells you that the code around your your use of isNothing
doesn't matter. It's not a problem with types, or anything to do with the actual isNothing
function you're trying to call, and there's no way you can change the code around isNothing
to get this to work.
Variable not in scope
almost always means one of three things:
- You haven't imported the name you're trying to use
- You have accidentally misspelled the name you're trying to use
- You intended to define something with that name, but haven't done so yet
Changing any of the code surrounding your use of isNothing
isn't going to change any of those 3 problems no matter which it is. Even looking at that code isn't going to tell you anything relevant; just closely look at the spelling of the name in the error message to confirm you haven't just made a typo, and if not you know you need to look elsewhere.
In this case it's #1. There are a bunch of useful functions that are in the Haskell Prelude
which are automatically imported for you, so you're probably used to just using functions without importing them, but the "normal" case is that to use anything that's already defined you have to import it. isNothing
isn't in the Prelude
, so that means to use it you have to find out which module it is in and add an import declaration to make it available. (If that module is in a package that isn't already installed, you will also have to obtain the package; that's a question I'm not going to address here)
isNothing
comes from the Data.Maybe
module (in the base
package, which is always installed as part of installing GHC, so no worries there). So you need to use:
import Data.Maybe
If you're working in a file you need to add that to the top of the file (just after the module header, but before you define any names yourself; all imports must come before any of your own code). If you're using the interpreter you can just enter the import as a command.
That will bring all of the names defined in Data.Maybe
into scope. If you want more control, you can explicitly import only some of the names, like this:
import Data.Maybe ( isNothing, isJust, listToMaybe )