我试图对《真实世界》Haskell第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
可能是什么问题?
您希望将length
应用于调用LB.words
的结果,而不是应用于函数的LB.words
。尝试(length . LB.words)
。