F# Cumulative Product of an array -


Using F #, I would like to calculate the cumulative product of an array without any loop. First thoughts Array Folds and arrays will be used. Map does not see how I can use them. what is your advice? Or peharps using a recursive function? Many thanks in advance for your help.

If you need the product of all elements, then you actually fold:

< Pre> & gt; A = [| 1; 2; 3; 4; 5 |]; & Gt; One | & Gt; Array.Fold (*) 1 ;; This value: int = 120

If you need an intermediate (cumulative) result, you can use the scan scan takes every element in the array And applies the element to a function (in this case the product), and the previous cumulative results. Starting with the value of 1 for the accumulator, we receive:

  & gt; One | & Gt; Array.scan (*) 1 ;; This value is: int [] = [| 1; 1; 2; 6; 24; 120 |]  

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -