【问题标题】:Modification of Real World Haskell program to count number of words and lines using MapReduce修改 Real World Haskell 程序以使用 MapReduce 计算字数和行数
【发布时间】:2013-03-03 03:04:55
【问题描述】:

我正在尝试对 Real World 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

可能是什么问题?

【问题讨论】:

    标签: mapreduce haskell


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但不幸的是这不起作用。现在我得到了错误:
    • 无法匹配预期类型Int64' with actual type Int' 预期类型:[Int] -> Int64 实际类型:[Int] -> Int 在mapReduce', namely sum 的第四个参数中' 在表达式中: mapReduce rdeepseq (length . LB.words) rdeepseq sum
    • 哦,等等,当我将函数更改为 lineCount :: [LB.ByteString] -> Int 而不是 lineCount :: [LB.ByteString] -> Int64 时,它起作用了。谢谢!
    • 没问题!为了将来参考,如果您认为自己的代码是正确的,那么在遇到类型错误时删除自己的注释有时会很有用。有时会出现错误,有时甚至更令人兴奋的是,编译器会推断出比您想象的更通用的类型。
    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多