Hi,<br><br>I'm new to Erlang, so I apologize in advance for asking what might be obvious to some.<br><br>I've run into problem trying to use functions as guards in Erlang. I looked through the manual, and discovered that this isn't allowed by the language in case those functions have side-effects. The thing is, my functions do not have side effects. Is there a way to convey that message to the compiler and allow my functions to be used as guards? (Something along the lines of #pragma i_swear_this_function_has_no_side_effects).<br>
<br>I know that code can be rewritten to avoid this problem, however, and probably due to my limited experience with Erlang, the particular piece of code I have in mind would look really ugly and really complicated without the ability to do this. Below is a snippet of my code that I'm referring to, if anyone has any suggestions on how to make it work in Erlang and look elegant I'd appreciate it.<br>
<br>This code deals with fixing up a red black tree after a node has been deleted, and here's what I'm trying to do:-<br><br>% Algorithm from <a href="http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html">http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html</a><br>
<br>% Case B1<br>delfix({Ky, Vy, Cy, {Kx, Vx, bb, A, B}, {Kz, Vz, b, C, D}}) when (isBlack(C) and isBlack(D)) -><br>     blackToken({Ky, Vy, Cy, {Kx, Vx, b, A, B}, {Kz, Vz, r, C, D}});<br><br>% Above is one of many clauses for the delfix function. Below is the full isBlack function:-<br>
<br>% An empty node (or subtree) or a black node (or subtree) are black (true), everything else is red (false).<br>isBlack({}) -> true;<br>isBlack({_, b, _, _, _}) -> true;<br>isBlack(_) -> false.<br><br>Now, rewriting the code for delfix as an if-statement wouldn't work without modifying a bunch of other things, because if C or D aren't black, there are other different patterns (not shown above) that might match. Expanding the tuples C and the D into something along the lines of {Kc, Vc, b, Lc, Rc} to match looks ugly, and would ignore the case where either might be empty subtrees {}.<br>
<br>I hope that my question actually makes sense :) If you've come this far in the email then thanks for bearing with me and my long, newbie question.<br><br>On a side node, I tried to create an account at the trapexit forum; but once I reach the page after agreeing to the conditions of the forums I get a huge SQL error message - something is wrong with the whole registration mechanism....<br clear="all">
<br>Cheers,<br>/Fuad