<div dir="ltr">Regarding #2<div><br></div><div>You are using both "if" and "case" expressions for the same situation:</div><div><br></div><div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px">
<font face="courier new, monospace">  case coordinateIsGreaterThan(R, Head)<span style="white-space:pre-wrap">  </span>of</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">    true -> cull(C, R, Culled, Tail);</font></div>
<div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">    false -> cullOnMagnitude(C, R, D, Culled, Head, Tail)</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px">
<font face="courier new, monospace">  end</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px">...</div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">  if M > R -> cull(C, R, Culled, Tail);</font></div>
<div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">     true -> cull(C, R, [Head | Culled], Tail)</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px">
<font face="courier new, monospace">  end</font></div></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace"><br></font></div>Many people find "case" easier to understand and less error prone than "if", and in this situation you can easily use "case" instead:<div style="font-family:arial,sans-serif;font-size:15.555556297302246px">
<font face="courier new, monospace"><br></font></div><div><div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">  case M > R of</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px">
<font face="courier new, monospace">     true  -> cull(C, R, Culled, Tail);</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">     false -> cull(C, R, [Head | Culled], Tail)</font></div>
<div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace">  end</font></div><div style="font-family:arial;font-size:small"><font face="courier new, monospace"><br></font></div>
<div style="font-family:arial;font-size:small"><font face="courier new, monospace"><br></font></div><div style="font-family:arial;font-size:small">"if" does have its uses, but "case" fits my needs 99% of the time.<br>
</div><div><br></div><div>Other than that I think your code looks fine.</div><br>Cheers,<br>Dan.</div></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace"><br>
</font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace"><br></font></div><div style="font-family:arial,sans-serif;font-size:15.555556297302246px"><font face="courier new, monospace"><br>
</font></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Feb 7, 2014 at 1:25 AM, kraythe . <span dir="ltr"><<a href="mailto:kraythe@gmail.com" target="_blank">kraythe@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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 style="white-space:pre-wrap">  </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>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>