简体   繁体   English

在 Maybe 中使用 isNothing function 时出现 Haskell 错误

[英]Haskell error when using the isNothing function in Maybe

I am trying to use isNothing in my code in Haskell, and it is giving me the error我正在尝试在 Haskell 中的代码中使用 isNothing,它给了我错误

<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.如果我将isNothing value替换为value == Nothing ,这就可以正常工作,所以我很困惑为什么以前的不起作用。

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 .这意味着编译器只是不知道任何名为isNothing的东西。

That immediately tells you that the code around your your use of isNothing doesn't matter .这会立即告诉您,围绕您使用isNothing的代码无关紧要 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.这不是类型的问题,也不是与您尝试调用的实际isNothing function 有关的任何问题,并且您无法更改isNothing周围的代码来使其正常工作。

Variable not in scope almost always means one of three things: Variable not in scope几乎总是意味着以下三种情况之一:

  1. You haven't imported the name you're trying to use您尚未导入您尝试使用的名称
  2. You have accidentally misspelled the name you're trying to use您不小心拼错了要使用的名称
  3. 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.更改任何围绕您使用isNothing的代码都不会改变这 3 个问题中的任何一个,无论它是什么。 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.在这种情况下,它是#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. Haskell Prelude中有一堆有用的函数,它们会自动为您导入,因此您可能习惯于只使用函数而不导入它们,但“正常”情况是使用您已经定义的任何东西导入它。 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. isNothing不在Prelude中,因此这意味着要使用它,您必须找出它在哪个模块中并添加导入声明以使其可用。 (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) (如果该模块位于尚未安装的 package 中,您还必须获取 package;这是一个我不打算在这里解决的问题)

isNothing comes from the Data.Maybe module (in the base package, which is always installed as part of installing GHC, so no worries there). isNothing来自Data.Maybe模块(在base package 中,它总是作为安装 GHC 的一部分安装,所以不用担心)。 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).如果你在一个文件中工作,你需要将它添加到文件的顶部(就在模块 header 之后,但在你自己定义任何名称之前;所有导入必须在你自己的代码之前)。 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:这会将Data.Maybe中定义的所有名称带入 scope。如果您想要更多控制,可以显式仅导入部分名称,如下所示:

import Data.Maybe ( isNothing, isJust, listToMaybe )

The function isNothing is not part of the standard prelude . function isNothing不是标准序曲的一部分。 Rather, it's distributed as part of the Data.Maybe module.相反,它作为Data.Maybe模块的一部分分发。 To use isNothing , you'll need to explicitly import that module:要使用isNothing ,您需要显式导入该模块:

import Data.Maybe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM