# [erlang-questions] Looking for a code review

Richard A. O'Keefe <>
Wed Sep 10 08:06:00 CEST 2008

```On 10 Sep 2008, at 4:19 pm, Bill McKeeman wrote:

Xs = samples(N, B-Dx, Dx, [B]), % N+1 sample points
Fs = fvals(F, Xs, []), % N+1 sample values
...
samples(0, _M, _Dx, L) -> L;
samples(N, M, Dx, L) -> samples(N-1, M-Dx, Dx, [M|L]).

fvals(_F, [], L) -> L;
fvals(F, [X|Xs], L) -> [F(X) | fvals(F, Xs, L)].
I'd be inclined to write
Xs = [A+I*Dx || I <- lists:seq(0, N)],
Fs = [F(X)   || X <- Xs],

a message from a worker; that means you cannot use this code
in any process that might receive a message from anywhere else.
This is the only thing that really *NEEDS* fixing.

I'd be inclined to fold together launching and spawning; what
was the reason for separating them?

start([_], [_], _, _, Workers) -> Workers;
start([X1|Xs=[X2|_]], [F1|Fs=[F2|_]], Tol, Self, Workers) ->
Worker = spawn(fun () -> worker(X1, X2, F1, F2, Tol, Self) end),
start(Xs, Fs, Self, [Worker|Workers]).

In fact, we don't need these lists.  So

run(F, A, B, Tol, N) ->
Dx = (B - A)/B,
Self = self(),
|| Worker <- start(0, N, A, A, Dx, F(A), Tol, Self, [])]).

start(N, N, _. _, _, _, _, Workers) ->
Workers;
start(I, N, X1, A, Dx, F1, Tol, Self, Workers) ->
J = I + 1,
X2 = I*Dx + A,
F2 = F(X2),
Worker = spawn(fun () -> worker(X1, X2, F1, F2, Tol, Self) end),
start(J, N, X2, A, Dx, F2, Tol, Self, [Worker|Workers]).

worker(X1, X2, F1, F2, Tol, Master) ->
....
Master ! Value.

```