I am working on a particle simulation program using Haskell. For one of the functions I am trying to determine the new velocities of all the particles in the simulation based on the mass and velocities of all the surrounding particles.
The function is of this form:
accelerate :: Float -> [Particle] -> [Particle]
Particle is a data type that contains the mass, position vector and velocity vector, the 'Float' argument represents the delta time of the respective time step in the simulation
I would like some suggestions on possible functions I can use to traverse the list while calculating the velocities of each of the particles with respect to the other particles in the list.
One possible approach I can think of:
assume there is another function 'velocityCalculator' which has the following definition:
velocityCalculator :: Particle -> Particle -> (Float,Float)
This takes two particles and returns the updated velocity vector for the first particle.
apply foldl; using the above function as the binary operator, a particle and the list of particles as the arguments, i.e.
foldl velocityCalculator particle particleList
iterate through the list of particle, applying foldl to each element and building the new list containing the particles with the updated velocities
I am not sure if this is the most efficient method so any suggestions and improvements are very much appreciated.
PLEASE NOTE -> as I have said I am only looking for suggestions not an answer!
Thanks!