Re: product() unexpected result
- Posted by petelomax Apr 29, 2021
- 1003 views
Can you provide me with a practical example of where product({}) returning 0 would actually be useful? [For me docs]
(you can ignore this now:)
See also https://en.wikipedia.org/wiki/Empty_product
In a quick search I found that Haskell and Python are quite adamant the answer should be 1.
Update: I've just found a still-commented-out function I wrote in mpfr.e:
function mpz_vecprod(sequence s, object zlr=1) -- -- Fast vector multiplication. -- Multiplying the vector elements in pairs is much faster for essentially -- much the same reason that merge sort is faster than insertion sort. -- Improved rosettacode/Primorial_numbers from 6 minutes to 6 seconds!!!! -- NB: Input sequence s (must all be mpz) is damaged. Returns an mpz. -- Obviously zlr allows you to specify the result when {} is passed, -- since I imagine there'll be cases where you'd rather get a zero, -- and just like mpz_init(), zlr can be an integer, atom, or string. --
So maybe the answer is for product() to have an optional second parameter, as above defaulting to 1.
(See https://rosettacode.org/wiki/Primorial_numbers#Phix for the precursor to that, without the zls parameter)
(Note that mpz_vecproc() is/will be obliged to return an mpz, so zlr must be "numeric", but that's not necessarily true for product(), though probably still a good idea?)