Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell Map.lookup输入时的奇怪行为_Haskell_Types - Fatal编程技术网

Haskell Map.lookup输入时的奇怪行为

Haskell Map.lookup输入时的奇怪行为,haskell,types,Haskell,Types,我一直在Haskell中使用Map.lookup,不断出现以下错误: Couldn't match expected type `[Char]` with actual type `Maybe String`. 有没有一种快速简单的转换方法?在没有看到代码的情况下,无法确定,但很可能您在地图中查找了某个内容,并希望得到与[Char]相同的字符串。事实上,lookup返回一个Maybe字符串,因此如果请求的键不在映射中,它将不会返回任何内容。您无法将x::Maybe字符串转换为字符串,除非您决定

我一直在Haskell中使用Map.lookup,不断出现以下错误:

Couldn't match expected type `[Char]` with actual type `Maybe String`.

有没有一种快速简单的转换方法?

在没有看到代码的情况下,无法确定,但很可能您在地图中查找了某个内容,并希望得到与[Char]相同的字符串。事实上,lookup返回一个Maybe字符串,因此如果请求的键不在映射中,它将不会返回任何内容。

您无法将x::Maybe字符串转换为字符串,除非您决定如何处理x为Nothing的情况—在您的情况下,当映射中的元素未找到时

试着这样做:

case Map.lookup ... of
   Nothing  -> ... -- handle the "not found" case
   Just str -> ... -- handle the "found" case, str contains the found string value

还有一个函数,在某些情况下可以提供更短的选择,但我建议先学习如何使用case。

如果可以显示再现上述错误的代码,这里的人员可以更好地帮助您。请注意,String是[Char]的别名.我想向初学者指出,这突出了Haskell最好的一面。Haskell迫使程序员提前考虑不愉快的路径,并明确地处理它。这与大多数其他编程语言背道而驰,但它会产生更可靠的程序。