Python from Wise Guy's Viewpoint

ketil+news at ii.uib.no ketil+news at ii.uib.no
Tue Oct 28 06:18:35 EST 2003


Matthew Danish <mdanish at andrew.cmu.edu> writes:

> 1 is an integer.  Simple type inference.

Okay, I'm not familiar enough with ML, and I didn't know that it won't
let you use 1 to represent 1.0 or 1/1 or whatever.

>>> Lisp gets exact rational arithmetic right, why don't ML or Haskell?

>> Could you point to a case where they don't?  I don't understand your 
>> criticism at all.  Is the ability to do modulo arithmetic "wrong"?

> - fun fact 0 = 1 | fact n = n * fact (n - 1);
> val fact = fn : int -> int
> - fact 10;
> val it = 3628800 : int
> - fact 15;
> val it = 1307674368000 : int  (* ideally *)

Here's Haskell as provided by GHCi:

    Prelude> let fact n = if n == 0 then 1 else n * fact (n-1)
    Prelude> fact 10
    3628800
    Prelude> :i it
    -- it is a variable
    it :: Integer
    Prelude> fact 15
    1307674368000

So '10' defaults to Integer, which is infinite precision.
You can of course also do:

    Prelude> fact 10.0
    3628800.0

> - 1 * 1.0;
> val it = 1.0 : float  (* ideally *)

    Prelude> 1*1.0
    1.0

The 1 is treated as a Float, since that is what * requires

> - 2 / 4;
> val it = 1/2 : ratio  (* ideally *)

    Prelude> 2/4
    0.5
    Prelude> (2/4  :: Rational)
    1 % 2

The default is Float, but Rational is there if that's what you want

> - 2 truncate 4;
> val it = 0 : int

I assume this means:

    Prelude> 2 `div` 4
    0
    Prelude> :i it
    -- it is a variable
    it :: Integer

If I understood you correctly, Haskell doesn't get it all that wrong?

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants




More information about the Python-list mailing list