当加载以下模块时,我会得到错误:
没有(Eq行星)的例子,因为在表达式中使用了‘==’·:==行星水星在一个模式保护器中,用于“ageOn”的一个方程:
我如何检查行星是否等于某颗行星?
module SpaceAge (Planet(..), ageOn) where
data Planet = Mercury
| Venus
| Earth
| Mars
| Jupiter
| Saturn
| Uranus
| Neptune
mercury :: Float
mercury = 0.2408467
venus :: Float
venus = 0.61519726
earth :: Float
earth = 1.0
mars :: Float
mars = 1.8808158
jupiter :: Float
jupiter = 11.862615
saturn :: Float
saturn = 29.447498
uranus :: Float
uranus = 84.016846
neptune :: Float
neptune = 164.79132
ageOn :: Planet -> Float -> Float
ageOn planet seconds
| planet == Mercury = seconds * mercury
| planet == Venus = seconds * venus
| planet == Earth = seconds * earth
| planet == Mars = seconds * mars
| planet == Jupiter = seconds * jupiter
| planet == Saturn = seconds * saturn
| planet == Uranus = seconds * uranus
| planet == Neptune = seconds * neptune发布于 2021-03-14 22:34:57
ageOn planet seconds = seconds * case planet of
Mercury -> mercury
Venus -> venus
Earth -> earth
Mars -> mars
...或者更好的是,考虑到向上的因素。
yearLength :: Planet -> Float
yearLength = \case
Mercury -> mercury
Venus -> venus
...
ageOn planet seconds = yearLength planet * seconds同时,我认为你的逻辑可能有错误。如果我活了一个地球年,我就活了超过一个水星年。因此,根据您的函数的确切含义,您可能需要定义
ageOn planet earthYears = earthYears / yearLength planethttps://stackoverflow.com/questions/66629663
复制相似问题