How could be optimized this code further:
let rec nCollect func = function
| [] -> []
| x::xs -> let firstAndNext body (firstBody, ys) =
let newFirst, z = func firstBody body
newFirst, z::ys
let y, ys = List.foldBack firstAndNext xs (x, [])
y::(nCollect func ys)
This code is part of nbody simulation program. It is getting each body and is applying a func function between it and each next. Results are used for next iterations. I optimized it slightly with lists. The problem is that the input bodies are under 10 in count, but nCollect is called millions times. For example if I use tail recursion in nCollect the result is worse.
I think that the answer to 90% of all micro-optimization problems in every language is always the same: use arrays, loops, and mutation.
So, I would use arrays, loops, and mutation, rather than
List.foldBack.