[erlang-questions] Trying to learn the Erlang Way
kraythe .
<>
Fri Feb 7 08:25:03 CET 2014
I am a newbie to erlang so please excuse the newbie questions. To learn the
language I have a use case, simply to take a vector C which defines the
center of a sphere with radius R and then cull a list of vectors and return
only the vectors in the sphere. A simple math problem the most efficient
method is to calculate the difference in C and a vector V and if any
component is greater than R then the vector can't be in the sphere, if not
then we have to do the expensive sort magnitude calculation.
To this end I have created a simple set of functions:
-module(vector3d).
-author("rsimmons").
%% API
-export([subtract/2, magnitude/1, scale/2, cull/3]).
%% Subtract the second vector from the first
subtract({X1, Y1, Z1}, {X2, Y2, Z2}) -> {(X1 - X2), (Y1 - Y2), (Z1 - Z2)}.
%% Compute the magnitude of the vector
magnitude({X, Y, Z}) -> math:sqrt((X * X) + (Y * Y) + (Z * Z)).
%% Determines if any coordinate in the given vector is bigger than the
passed in value V.
coordinateGreaterThan(V, {X, Y, Z}) when X > V; Y > V; Z > V -> true;
coordinateGreaterThan(_, _) -> false.
%% Culls the list of vectors X to only those that are in the sphere devined
by vector C as a center and R as a radius.
cull(C, R, Vectors) when is_number(R), is_tuple(C) -> cull(C, R, Vectors,
[]).
cull(C, R, Culled, []) -> Culled;
cull(C, R, Culled, [Head|Tail]) ->
D = subtract(C, Head),
case coordinateIsGreaterThan(R, Head) of
true -> cull(C, R, Culled, Tail);
false -> cullOnMagnitude(C, R, D, Culled, Head, Tail)
end.
cullOnMagnitude(C, R, D, Culled, Head, Tail) when is_tuple(D), is_number(R)
->
M = magnitude(D),
if M > R -> cull(C, R, Culled, Tail);
true -> cull(C, R, [Head | Culled], Tail)
end.
When I load these into a file and compile them I get the following result:
16> c(vector3d).
vector3d.erl:53: function coordinateIsGreaterThan/2 undefined
vector3d.erl:45: Warning: function coordinateGreaterThan/2 is unused
vector3d.erl:50: Warning: variable 'C' is unused
vector3d.erl:50: Warning: variable 'R' is unused
Note that there are some other methods in this file so the line numbers
will vary. My questions are:
1) Why is it saying my coordinateIsGreaterThan/2 method is undefined when I
can see it here and then to say it is unused in the next line? That seems
contradictory?
2) Is this in the direction of the "erlang way" to do things?
3) I understand the variables not being used but it seems odd to me from
the java language world to not define the names for the variables. Is this
normal in erlang?
Thanks in advance.
*Robert Simmons Jr. MSc.*
*LinkedIn: **http://www.linkedin.com/pub/robert-simmons/40/852/a39
<http://www.linkedin.com/pub/robert-simmons/40/852/a39>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140207/5215fcdd/attachment.html>
More information about the erlang-questions
mailing list