我还是个新手,试图创建一个在函数中使用的列表,并希望使其尽可能小,恰好是 logBase x y。
但我无法将 logBase 转换为我可以在此列表中使用的内容。
[1 ..(logBase x y)]
有什么建议么?
您没有发布您收到的类型错误,但我想它是这样的:
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]
使用截头、天花板或地板。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)