2010-07-16 10 views
5

qui ho una breve funzione di haskell che dovrebbe convertire "ABCDEF" in 0x41,0x42,0x43,0x44,0x45,0x46 (i loro valori ascii), quindi moltiplicarli in modo che diventi 0x4142,4344,4546 ma esso sembra limitare la lunghezza intera - pensavo che haskell usasse arbitrari bignum?La funzione Haskell sembra limitare la lunghezza intera - pensavo che usasse i bignum?

L'ultima riga del codice funziona bene, che mi

Tutte le idee puzzle? Grazie mille

import Data.Char 
import Numeric 

strToHex2 (h:[]) = ord h 
strToHex2 (h:t) = (ord h) + ((strToHex2 t) * 256) 
strToHex s = strToHex2 (reverse s) 

main = do 
    print(strToHex "ABCDEF") 
    print ((((((((0x41*256+0x42)*256)+0x43)*256)+0x44)*256)+0x45)*256+0x46) 

L'output è:

1128547654  <- limited to 32 bits for some reason? 
71752852194630 <- that's fine 
+1

Vedere anche http://stackoverflow.com/questions/3429291/haskell-int-and-integer –

+0

I tipi sono tuoi amici. Aggiungi le firme del tipo, ed è chiaro cosa sta succedendo! –

risposta

10

tuo problema è che ord restituisce un Int, che è a larghezza fissa. Vuoi toInteger $ ord h.

+1

Quindi c'è una differenza tra un Int e un intero? – Chris

+2

@Chris Sì, vedere http://www.haskell.org/tutorial/numbers.html – chollida

Problemi correlati