我正在尝试用 Haskell 编写幂级数,
e^x = 1 + x + x^2/2! + x^3/3! + ...
这样它就会输出
[1,1,1/2,1/6,...]
到目前为止,我得到了:
factorial 0 = 1
factorial n = n * factorial (n - 1)
powerSrs x = 1 : powerSrsFunc[1..] where
powerSrsFunc ( p: xs ) =
p : powerSrsFunc[y | y <-xs, ( (x^y) / (factorial y) )]
但是,我知道我在这里的输入是错误的。我收到此错误:
tut08.hs:8:58:
No instance for (Integral Bool)
arising from a use of `^'
Possible fix: add an instance declaration for (Integral Bool)
In the first argument of `(/)', namely `(x ^ y)'
In the expression: ((x ^ y) / (factorial y))
In a stmt of a list comprehension: ((x ^ y) / (factorial y))
tut08.hs:8:62:
No instance for (Fractional Bool)
arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Bool)
In the expression: ((x ^ y) / (factorial y))
In a stmt of a list comprehension: ((x ^ y) / (factorial y))
In the first argument of `powerSrsFunc', namely
`[y | y <- xs, ((x ^ y) / (factorial y))]'
1)如何在 Haskell 中编写分数,使其输出类似于“1/2”?
2) 当他们说 (Integral Bool) 和 (Fractional Bool) 没有实例时是什么意思?
它是指两个类型为 Integral 和 Bool 的参数吗?
不需要积分和积分吗?