【发布时间】:2018-05-10 02:53:57
【问题描述】:
我是 Haskell 的新手。我把它输入到 WinGHCi 中,它工作正常:
> let x = 0.5
> let n = 5
> map (\y->(x**y)) [0..n]
[1.0,0.5,0.25,0.125,6.25e-2,3.125e-2] -- notice it is powers of 1/2 !
但是当我在文件中定义一个简单的函数时:
powersOfx :: (Integral a, Floating b) => a -> b -> [b]
powersOfx n x = map (\y->(x**y)) [0..n]
然后输入
:l myFile,我明白了:
Couldn't match expected type ‘b’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for:
powersOfx :: forall a b. (Integral a, Floating b) => a -> b -> [b]
发生了什么事?我做的签名不正确吗?我猜我可能是,因为当我把它注释掉并且 :l myFile 它工作但
:t powersOfx
我明白了:
powersOfx :: (Floating b, Enum b) => b -> b -> [b]
注意“枚举”而不是“积分”。
我想我可以摆脱类型签名,但我的印象是放置签名是一种很好的做法,我正在尝试解决一个更大的问题,在这里我收到错误报告: Ambiguous type variable `a0' arising from a use of `it'
如果我让这部分工作,我将发布一个单独的问题!
请让我知道我是否会更好地发布到另一个组,或者我是否应该发布更多信息。
-戴夫
【问题讨论】:
-
它必须是
Enum,否则你不能进行[0..n]的操作。至于Floating——**的操作数必须是同一类型。也许您正在寻找^运算符?另外,powersOf x = map (x^) [0..]你也可以take 5 (powersOf 0.5)
标签: haskell