map.insert提示没有理由的Haskell中的错误
这是我使用的函数:
threeWordFunction :: [String] -> Map.Map String [String] -> String
threeWordFunction [x, y, z] orbitingData = do
Map.insert z [x, y] orbitingData
""
导致以下错误:
src\MyLib.hs:40:6: error:
* Couldn't match type `Map.Map String' with `[]'
Expected type: [[String]]
Actual type: Map.Map String [String]
* In a stmt of a 'do' block: Map.insert z [x, y] orbitingData
In the expression:
do Map.insert z [x, y] orbitingData
""
In an equation for `threeWordFunction':
threeWordFunction [x, y, z] orbitingData
= do Map.insert z [x, ....] orbitingData
""
|
40 | Map.insert z [x, y] orbitingData
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我尝试了大量修复程序,但没有发现可能导致此错误的任何修复程序。该功能只是试图将字符串添加到地图上,没有什么花哨的。任何帮助将不胜感激。
This is the function I am using:
threeWordFunction :: [String] -> Map.Map String [String] -> String
threeWordFunction [x, y, z] orbitingData = do
Map.insert z [x, y] orbitingData
""
Resulting in the following error:
src\MyLib.hs:40:6: error:
* Couldn't match type `Map.Map String' with `[]'
Expected type: [[String]]
Actual type: Map.Map String [String]
* In a stmt of a 'do' block: Map.insert z [x, y] orbitingData
In the expression:
do Map.insert z [x, y] orbitingData
""
In an equation for `threeWordFunction':
threeWordFunction [x, y, z] orbitingData
= do Map.insert z [x, ....] orbitingData
""
|
40 | Map.insert z [x, y] orbitingData
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I've tried a whole bunch of fixes but found nothing that could cause this error. The function is just trying to add the strings to the map, nothing fancy. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题
正如一些贡献者所说,
do
是问题所在。但是,让我尝试分解您想要的东西(如我所见)以及您的工作。
声明您的功能类型。这是您打算做的。使用
String
和MAP
的列表,您需要提供String
。如果我阅读了这篇文章,在Haskell中,我将推断您可能要使用MAP
提取有用的东西来构建String
。实际上,函数的唯一输出是String
。由于地图
不是输出,任何修改都会被“丢失”。当我阅读您想做的事情时,您声称要在
MAP
中插入一个值。因此,我想您要输出结果地图
。在这里,我可以推断出您的第一个错误:您会以副作用思考。我想您想在功能中修改map
,并且您希望该地图将在功能之外进行修改。但这不是哈斯克尔的工作方式。这就是大多数具有势在必行功能的语言的工作方式。因此,要 do 您想使用
do
Word。此背后的心态是提供一系列教学,例如在带有卷曲括号的C块中。这是错误的。在Haskell中,do
单词是语法糖,并指出了您在单月中的事实……因此您的计算结果可能是单月。但这不是功能类型所说的。因此,编译器会产生错误。考虑到类型,您可以做些什么才能与您的类型一致
,解决方案将是Chi提供的解决方案。
编译器将不再抱怨。您可以构建一个新的地图
newMap
,该可以按照所需的方式进行修改。并且您提供字符串
“”
作为输出。但是,newMap
在您的功能之外永远无法达到。因此插入毫无用处。您该怎么做才能实现您的需求
更改类型... Haskell中的类型提供了一个规范,可以清楚地定义预期的有效解决方案(或多或少)。
因此,您可以需要明确提供地图的东西。
或地图和字符串,
您也可以使用诸如状态之类的单元,但对于您的水平来说可能太多了。
考虑到新类型,您现在可以返回所需的实体。
NB如果您始终想提供
“”
作为字符串,则可能表明您不需要它。希望这将帮助您理解问题。
The problem
As some contributors says, the
do
is the problem.However, let me try to decompose what you want (as I can see) and what you do.
declares the type of your function. It is what you plan to do. With a list of
String
and aMap
you want to provide aString
. If I read this, in Haskell, I will deduce that you probably want to use theMap
to extract something useful to build aString
. Indeed, the only output of your function is aString
. Since theMap
is not an output any modification will be "lost".When I read what you want to do, you claimed that you want to insert a value in the
Map
. So I suppose that you want to output your resultingMap
. And here I can deduce your first error : you think with side effect. I suppose that you wanted to modify theMap
inside your function, and you've expected that the Map will be modified outside the function. But it is not how Haskell works. This is how most languages with imperative features works.So to do this you want to use a
do
word. The state of mind behind this is to provide a sequence of instruction like in a C-block with curly braces. And this is wrong. In Haskell, thedo
word is syntaxic sugar and points the fact that you are in a monad... So the result of your computation will probably be a monad. But this is not what the function type said. So the compiler produces an error.What can you do to be consistent with your type
Considering the type, a solution will be the one provide by Chi.
The compiler will complain no more. You build a new Map
newMap
that is modify the way you want. And you provide theString
""
as an output. However, thenewMap
will never be reachable outside your function. So the insert is useless.What can you do to achieve your need
Change the type... The type in Haskell provide a specification that clearly defines the expected valid solutions (more or less).
So you can want something that explicitly provide a Map.
or a Map and a String
You can also use monad like State but it may be too much for your level.
Considering the new type, you can now return an entity that is what you need.
NB if you always want to provide a
""
as a String, this probably indicates that you don't need it.Hope this will help you understand the problem.