Possible Duplicate:
Alternatives to procedural loops and iterating over lists in Mathematica
I am new to Mathematica and not familiar with functional programming. In particular, I have no idea on how to replace the usual for
loops in C++. Here is an example of the way I am writing code now:
InitCoords[] := {
VDiv[gap, region, initCell];
n = 0;
For[nz = 0, nz < initCell[3], nz++,
For [ny = 0, ny < initCell[2], ny++,
For[nx = 0, nx < initCell[1], nx++,
VSet[c, nx + 0.25, ny + 0.25, nz + 0.25];
VMul[c, c, gap];
VSAdd[c, c, -0.5, region];
For[j = 0, j < 4, j++,
Do[r[n][i] = c[i], {i, 1, NDIM}];
If[j != 3,
If[j != 0, r[n][1] += 0.5*gap[1];];
If[j != 1, r[n][2] += 0.5*gap[2];];
If[j != 2, r[n][3] += 0.5*gap[3];];
];
++ n;
];
];
];
];
}
I know that this is a very bad way to code in Mathematica. What are the alternatives then?
Another question. Is there is a Mathematica convention for defining a subroutine with no input? I want to partition my big program into smaller parts. Currently, I am doing it by writing a function with no input.
VDiv
,VSet
,VMul
, andVSAdd
do? $\endgroup$