我还是新手,正在尝试创建一个用于函数的列表,并希望它尽可能小,恰好是 logBase x y。但我无法将 logBase 纳入我可以在此列表中使用的内容。
[1 .. (logBase xy)]
有什么建议么?
我还是新手,正在尝试创建一个用于函数的列表,并希望它尽可能小,恰好是 logBase x y。但我无法将 logBase 纳入我可以在此列表中使用的内容。
[1 .. (logBase xy)]
有什么建议么?
你没有发布你得到的类型错误,但我想它是这样的:
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]
使用 truncate、celing 或 floor。
您可能需要某种舍入、截断、下限或上限函数。Ints 和 Floats 是不同的类型(如您所见),编译器不会让您混合使用它们。我会在一分钟内找到参考。