2011-12-21 7 views

risposta

23

In Haskell non si converte in ma da. Vedi fromRational.

threeHalves :: Ratio Integer 
threeHalves = 3 % 2 
sqrt threeHalves -- Fails 
sqrt $ fromRational threeHalves -- Succeeds 

Se avete bisogno di una funzione Rational -> Float, è possibile definire come

toFloat x = fromRational x :: Float 
+2

Grazie, mi piace soprattutto la prima frase .. dovrebbe aiutare molto in futuro. – aelguindy

+3

A volte converti, a volte a. C'è, ad esempio, 'toInteger'. – augustss

2

C'è anche fromIntegral per convertire Int s e Integer s per qualsiasi istanza di Num.

foo :: Float -> Float 
foo x = x+1 

value :: Int 
value = 4 

newValue = foo (fromIntegral value) 
2

Nota che Rational è un tipo, non un typeclass, quindi Rational a => a non ha senso. Prova a digitare Hoogling Rational -> Float.