haskell - 函数在应用程序中产生“类型错误”消息(Haskell)
问题描述
我正在编写这个函数,旨在计算缓存索引所需的位数。它使用如下所示的公式和类型。
问题是它会产生以下错误消息。我怎样才能解决这个问题 ?
源代码:
getNumBits :: (Integral a, RealFloat a1) => a1 -> [Char] -> [c] -> a
getNumBits fromIntegral(numOfSets) cacheType (x:xs)
| (cacheType=="fullyAssoc") = 0
| (cacheType=="setAssoc") = fromIntegral(logBase2(fromIntegral(length (x:xs)))/numOfSets)
| (cacheType=="directMap") = fromIntegral(logBase2(fromIntegral(length (x:xs))))
错误信息:
ERROR file:.\HaskellProject.hs:27 - Type error in application
*** Expression : fromIntegral (logBase2 (fromIntegral (length (x : xs))) / numOfSets)
*** Term : logBase2 (fromIntegral (length (x : xs))) / numOfSets
*** Type : [Char]
*** Does not match : Int
解决方案
getNumBits fromIntegral(numOfSets) cacheType (x:xs)
这不是haskell,你从哪里学来的?您不能以这种方式将函数应用于参数。相反,将参数绑定到变量并在正文或where
子句中应用函数:
getNumBits numOfSetsFloat cacheType (x:xs)
...
where numOfSets = fromIntegral numOfSetsFloat
此外,您还需要处理最后一个参数的空列表案例,否则当有人将函数调用为getNumBits 0 [] []
.
推荐阅读
- java - 错误:未知列“0_”。在“where子句”中
- r - 如何将以本地语言表示的字符串布尔值转换为布尔值
- python - 城市、道路和网络流量。通过另一个表中的索引列对一个表进行分组/聚合?
- azure - 无法更改 Azure ML 笔记本中的虚拟环境
- sql - 使用“create as”语句创建索引组织表的 Oracle 问题
- excel - GetSaveAsFilename 使用单元格值作为文件标题
- java - 如何调试 javax.net.ssl 包
- javascript - 尽管在 Postman 工作,但出现 400 错误,可能的原因是什么?
- powershell - 是否可以使用 Powershell 从模板创建 Microsoft 团队?
- arrays - 如何根据 IlNumerics 中的数组中的多个条件过滤行