[Haskell] big integer in haskell

Claus Reinke claus.reinke at talk21.com
Fri Apr 15 19:46:00 EDT 2005


Hi,

the difference is that you restrict your function to Int,
whereas the default type is Integer. See:

http://www.haskell.org/onlinereport/decls.html#sect4.3.4

and

http://www.haskell.org/onlinereport/basic.html#sect6.4

hth,
claus

btw, you can ask ghci to give you the type of each 
expression you evaluate:

Prelude> :set +t

Prelude> 2^128
340282366920938463463374607431768211456
cmTypeOfName: it
it :: Integer

Prelude> 2^128 :: Int
0
cmTypeOfName: it
it :: Int


Prelude> maxBound :: Int
2147483647
cmTypeOfName: it
it :: Int
Prelude> maxBound :: Integer

<interactive>:1:0:
    No instance for (Bounded Integer)
      arising from use of `maxBound' at <interactive>:1:0-7
    Probable fix: add an instance declaration for (Bounded Integer)
    In the expression: maxBound :: Integer
    In the definition of `it': it = maxBound :: Integer




----- Original Message ----- 
From: "ting wang" <tting.wang at gmail.com>
To: <haskell at haskell.org>
Sent: Saturday, April 16, 2005 12:03 AM
Subject: [Haskell] big integer in haskell


> Hello all,
> I am a new learner, and i am trying to write a function to calculate
> factorial of a natural number:
> (I use ghc 6.4, window xp)
> fac :: Int -> Int
> fac n
> | n == 0 = 1
> | n > 0 = fac (n-1) * n
> | otherwise = 0
> this works when n < 14 and result is 1278945280, when n > 14 the answer
> is not right n = 15 result is 2004310016. I know it is maybe because of
> overflow. So i calclate 2^32, 2^64, 2^128 the result correct and is much
> bigger as 14!,Why? 2^n use some big integer or my implementation of n!
> is not correct, I'm really confused.
> 
> Someone can help me?
> 
> Thanks
> Ting Wang
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell



More information about the Haskell mailing list