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程序,使用MapReduce计算字数和行数_Mapreduce_Haskell - Fatal编程技术网

修改真实世界的Haskell程序,使用MapReduce计算字数和行数

修改真实世界的Haskell程序,使用MapReduce计算字数和行数,mapreduce,haskell,Mapreduce,Haskell,我试图对真实世界哈斯克尔第24章中的一个程序做一些简单的修改。下面是一些计算文件中行数的代码: import qualified Data.ByteString.Lazy.Char8 as LB lineCount :: [LB.ByteString] -> Int64 lineCount = mapReduce rdeepseq (LB.count '\n') rdeepseq sum 我正试图写一些代码来计算文件中的字数。以下是我的想法:

我试图对真实世界哈斯克尔第24章中的一个程序做一些简单的修改。下面是一些计算文件中行数的代码:

import qualified Data.ByteString.Lazy.Char8 as LB
lineCount :: [LB.ByteString] -> Int64
lineCount = mapReduce rdeepseq (LB.count '\n')
                      rdeepseq sum
我正试图写一些代码来计算文件中的字数。以下是我的想法:

import qualified Data.ByteString.Lazy.Char8 as LB
lineCount :: [LB.ByteString] -> Int64
lineCount = mapReduce rdeepseq (length LB.words)
                      rdeepseq sum
然而,我得到:

Couldn't match expected type `LB.ByteString -> b0'
            with actual type `Int'
In the return type of a call of `length'
Probable cause: `length' is applied to too many arguments
In the second argument of `mapReduce', namely `(length LB.words)'
In the expression:
  mapReduce rdeepseq (length LB.words) rdeepseq sum

有什么问题吗?

您希望对调用
LB.words
的结果应用
length
,而不是对函数应用
LB.words
。请尝试
(length.LB.words)

谢谢,但不幸的是,这不起作用。现在我得到了错误:在
mapReduce'的第四个参数中,即表达式中的
sum':mapReduce rdeepseq(length.LB.words)rdeepseq sumOh wait中,无法将预期类型
Int64'与实际类型匹配,当我将函数改为lineCount::[LB.ByteString]->Int而不是lineCount::[LB.ByteString]->Int64时,它起作用了。谢谢没问题!作为将来的参考,如果您认为代码是正确的,那么在遇到类型错误时删除自己的注释有时是有用的。有时会出现错误,有时甚至更令人兴奋的是,编译器会推断出比您想象的更一般的类型。