for i in a..b do
res <- res * myarray.[i]
res
Do I have to use like
Array.fold (*) 1 (Array.sub myarray a (b - a + 1))
, which I believe is rather slow and not that concise?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Daniel’s solution is pretty neat and I think it should be nearly as efficient as the
forloop, because it does not need to clone the array.If you wanted a more concise solution, then you can use indexer instead of
Array.sub, which does need to clone some part of the array, but it looks quite neat:This clones a part of the array because the slicing operation returns an array. You could define your own slicing operation that returns the elements as
seq<'T>(and thus does not clone the whole array):Using this function, you could write:
I believe this more directly expresses the functionality that you’re trying to implement. As always with performance – you should measure the performance to see if you need to make such optimizations or if the first version is fast enough for your purpose.