【发布时间】:2014-10-17 01:02:01
【问题描述】:
我写了以下函数。
f :: Integer -> Integer
f x = if (odd x) then 0 else (floor . logBase 2) x
但是出现以下编译时错误:
F.hs:2:31: 没有因使用
floor' Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of(.)' 产生的 (RealFrac Integer) 实例,即 `floor' 在表达式中: floor 。对数库 2 在表达式中: (floor .logBase 2) xF.hs:2:39: 没有因使用
logBase' Possible fix: add an instance declaration for (Floating Integer) In the second argument of(.)' 产生的 (Floating Integer) 实例,即 `logBase 2' 在表达式中: floor 。对数库 2 在表达式中:(floor .logBase 2) x Failed, modules loaded: none.
如何正确编写上述函数?
【问题讨论】:
-
fromIntegral是你的朋友。 -
谢谢,卢基。这特别有用 - stackoverflow.com/a/1970515/409976。
-
floor . logBase 2=>floor . logBase 2 . fromIntegral -
为什么不想看奇数呢?日志适用于任何正数。
-
我没有指定的错误。从 2013 年开始,我正在做这个作业problem 5 - 用于自学。我应该更好地措辞这个问题
标签: haskell