我试图写在Haskell幂级数,电源系列在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)是什么意思时,他们说的(积分布尔)和(分数布尔)没有实例?
它是指两个参数是类型的积分和布尔?
这岂不是拿积分和积分?
我不能似乎得到的分数位运行的权利。看来Data.Ratio包中只有%操作符,它将显示比率为2%3而不是2/3。 – ali
嗯,我想你可以把它变成一个字符串并用%替换%。我的猜测是,它使用%,以便它可以正确读入。使用/会与/的标准使用冲突。 –
哦,男孩,现在我们将展开关于Int - > String对话的一系列新问题。哈哈。 – ali