0
我一直在努力學習的Haskell幾天,但我不能看到弄清楚如何創建應答/輸出這是一個浮動,以一個雙哈斯克爾從浮到雙
module SpaceAge (Planet(..), ageOn) where
-- calculate seconds to an age on different planets
data Planet = Earth
| Jupiter
| Mars
| Mercury
| Neptune
| Saturn
| Uranus
| Venus
-- one year has 31557600 seconds
orbitalPeriodInSeconds :: Planet -> Float
orbitalPeriodInSeconds planet = 31557600 * multiplier where
multiplier = case planet of
Earth -> 1.0
Jupiter -> 11.862615
Mars -> 1.8808158
Mercury -> 0.2408467
Neptune -> 164.79132
Saturn -> 29.447498
Uranus -> 84.016846
Venus -> 0.61519726
-- user input example: ageOn Venus 9303000
ageOn :: Planet -> Float -> Float
ageOn planet seconds = seconds/orbitalPeriodInSeconds planet
答案/輸出將是一個浮點數,但我需要得到一個double。 例
* 4,1> ageOn水星2134835688
280.87936
但我需要280.88
Offcourse如果我改變浮動爲Double,因爲它期待一個浮動的編譯器會引發錯誤。
我覺得有一個誤解,在這裏:'280.87936'和'280.88'可以都'Float' *或*'Doubles' 。你真的需要一個'Double',還是你想回答兩個小數點? –
'orbitalPeriodInSeconds'不需要返回'Float';它可以返回一個多態值,其類型是'Fractional'類類的一個實例。 ('orbitalPeriodInSeconds :: Fractional a => Planet - > a')。 'ageOn :: Fractional a => Planet - > a - > a'也是一樣。 – chepner
可能是http://stackoverflow.com/questions/1559590/haskell-force-floats-to-have-two-decimals –