Hacker News new | ask | show | jobs
by b2gills 1834 days ago
So does Raku

    say 3×(1÷3)
    # 1
It works because `1÷3` is a rational number.

    my \rat = 1÷3;

    say rat.numerator;   # 1
    say rat.denominator; # 3
If you multiply it by 3, you get another rational number which represents 1.

    my \result = 3×rat;

    say result; # 1

    say result.numerator;   # 1
    say result.denominator; # 1
---

Now that this may be slower than using floating point numbers, but it isn't that much slower. You only have two integers that you have to manage.

Actually on many older processors there wasn't built in support for floating point numbers, so it might actually have been faster to use rationals like this.

(I'm not sure if this is how APL handled it.)

---

APL happens to be one of the languages Raku copied ideas from

    ×/⍳10
Translated to Raku

    [×] 1..10
That is rather than `×/…` it is written `[×] …`. Partly because `/` is the ASCII version of the division operator.

We could go further towards replicating it.

    {
      sub prefix:<⍳> (UInt \n){ 1..n }
      sub infix:</> (&f, +list){
        [[&f]] list
      }
      constant term:<×> = &infix:<×>;

      say ×/⍳10;
      # 3628800
    }