<div dir="ltr"><div>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. </div>
<div><br></div><div><font face="courier new, monospace">To this end I have created a simple set of functions: </font></div><div><div><font face="courier new, monospace">-module(vector3d).</font></div><div><font face="courier new, monospace">-author("rsimmons").</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">%% API</font></div><div><font face="courier new, monospace">-export([subtract/2, magnitude/1, scale/2, cull/3]).</font></div>
</div><div><font face="courier new, monospace"><br></font></div><div><div><font face="courier new, monospace">%% Subtract the second vector from the first</font></div><div><font face="courier new, monospace">subtract({X1, Y1, Z1}, {X2, Y2, Z2}) -> {(X1 - X2), (Y1 - Y2), (Z1 - Z2)}.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">%% Compute the magnitude of the vector</font></div><div><font face="courier new, monospace">magnitude({X, Y, Z}) -> math:sqrt((X * X) + (Y * Y) + (Z * Z)).</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">%% Determines if any coordinate in the given vector is bigger than the passed in value V.</font></div><div><font face="courier new, monospace">coordinateGreaterThan(V, {X, Y, Z}) when X > V; Y > V; Z > V -> true;</font></div>
<div><font face="courier new, monospace">coordinateGreaterThan(_, _) -> false.</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">%% 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.</font></div>
<div><font face="courier new, monospace">cull(C, R, Vectors) when is_number(R), is_tuple(C) -> cull(C, R, Vectors, []).</font></div><div><font face="courier new, monospace">cull(C, R, Culled, []) -> Culled;</font></div>
<div><font face="courier new, monospace">cull(C, R, Culled, [Head|Tail]) -></font></div><div><font face="courier new, monospace"> D = subtract(C, Head),</font></div><div><font face="courier new, monospace"> case coordinateIsGreaterThan(R, Head)<span class="" style="white-space:pre"> </span>of</font></div>
<div><font face="courier new, monospace"> true -> cull(C, R, Culled, Tail);</font></div><div><font face="courier new, monospace"> false -> cullOnMagnitude(C, R, D, Culled, Head, Tail)</font></div><div><font face="courier new, monospace"> end.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">cullOnMagnitude(C, R, D, Culled, Head, Tail) when is_tuple(D), is_number(R) -></font></div><div><font face="courier new, monospace"> M = magnitude(D),</font></div>
<div><font face="courier new, monospace"> if M > R -> cull(C, R, Culled, Tail);</font></div><div><font face="courier new, monospace"> true -> cull(C, R, [Head | Culled], Tail)</font></div><div><font face="courier new, monospace"> end.</font></div>
</div><div><br></div><div>When I load these into a file and compile them I get the following result: </div><div><br></div><div><div><font face="courier new, monospace">16> c(vector3d).</font></div><div><font face="courier new, monospace">vector3d.erl:53: function coordinateIsGreaterThan/2 undefined</font></div>
<div><font face="courier new, monospace">vector3d.erl:45: Warning: function coordinateGreaterThan/2 is unused</font></div><div><font face="courier new, monospace">vector3d.erl:50: Warning: variable 'C' is unused</font></div>
<div><font face="courier new, monospace">vector3d.erl:50: Warning: variable 'R' is unused</font></div></div><div><br></div><div>Note that there are some other methods in this file so the line numbers will vary. My questions are: </div>
<div><br></div><div>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?</div><div>2) Is this in the direction of the "erlang way" to do things? </div>
<div>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? </div><div><br></div><div>Thanks in advance. </div>
<br clear="all"><div><div dir="ltr"><div style="font-family:arial;font-size:small"><b>Robert Simmons Jr. MSc.</b></div><div><i style="font-family:arial;font-size:small">LinkedIn: </i><font face="arial"><i><a href="http://www.linkedin.com/pub/robert-simmons/40/852/a39" target="_blank">http://www.linkedin.com/pub/robert-simmons/40/852/a39</a></i></font></div>
</div></div>
</div>