【发布时间】:2009-09-09 01:58:45
【问题描述】:
我还是新手,正在尝试创建一个用于函数的列表,并希望将其尽可能小,恰好是 logBase x y。 但我无法将 logBase 纳入我可以在此列表中使用的内容。
[1 .. (logBase x y)]
有什么建议吗?
【问题讨论】:
标签: haskell floating-point int
我还是新手,正在尝试创建一个用于函数的列表,并希望将其尽可能小,恰好是 logBase x y。 但我无法将 logBase 纳入我可以在此列表中使用的内容。
[1 .. (logBase x y)]
有什么建议吗?
【问题讨论】:
标签: haskell floating-point int
你没有发布你得到的类型错误,但我想它是这样的:
Prelude> let x = 2
Prelude> let y = 7
Prelude> [1 .. (logBase x y)]
<interactive>:1:7:
No instance for (Floating Integer)
arising from a use of `logBase' at <interactive>:1:7-17
Possible fix: add an instance declaration for (Floating Integer)
In the expression: (logBase x y)
In the expression: [1 .. (logBase x y)]
In the definition of `it': it = [1 .. (logBase x y)]
问题是:
Prelude> :t logBase
logBase :: (Floating a) => a -> a -> a
返回 Floating 类中的一个类型,而程序中的其他变量 (1, 'x', 'y') 是整数类型。
我想你想要一个整数序列?
Prelude> :set -XNoMonomorphismRestriction
Prelude> let x = 2
Prelude> let y = 42
Prelude> [1 .. truncate (logBase x y)]
[1,2,3,4,5]
使用截断、天花板或地板。
【讨论】:
您可能需要某种舍入、截断、下限或上限函数。 Ints 和 Floats 是不同的类型(如您所见),编译器不会让您混合使用它们。我会在一分钟内找到参考资料。
【讨论】: