Thanks for everyone for your responses. They were definitely helpful, I now understand Erlang and some of these issues a bit better.<br><br>I ended up using macros (as Gleb and Robert suggested; specifically the macro Gleb posted). I think this is a more elegant solution than doing any other transformations of the code, but I could be wrong...<br>
<br>Anyway, I uploaded my code to:-<br><a href="http://www.cs.auckland.ac.nz/%7Efuad/rbtree.erl" target="_blank">http://www.cs.auckland.ac.nz/~fuad/rbtree.erl</a><br><a href="http://www.altabba.org/2008/06/full-red-black-tree-implementation-in.html" target="_blank">http://www.altabba.org/2008/06/full-red-black-tree-implementation-in.html</a> (for some background)<br>
<br>If anyone has any comments about my code I would appreciate it.<br>
<br>Robert: I've based my delete algorithm on the one at <a href="http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html" target="_blank">http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html</a> , would definitely be interested in a faster algorithm. Is it the one you're using for the rbdict you posted?<br>
<br>Thanks again.<br clear="all">
<br>Cheers,<br>/Fuad
<br><br><div class="gmail_quote">On Sun, Jun 15, 2008 at 2:26 AM, Robert Virding <<a href="mailto:rvirding@gmail.com" target="_blank">rvirding@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2008/6/13 Fuad Tabba <<a href="mailto:fuad@cs.auckland.ac.nz" target="_blank">fuad@cs.auckland.ac.nz</a>>:<br><div class="gmail_quote"><div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks Doug and Jachym.<br><br>Jachym is right, Doug's suggestion would work if the guard was inside my function rather than at a top level context. I know that I could rewrite my code and convert it to a bunch of if statements, but I can't think of a way to write such code without it looking ugly.<br>
<br>Being able to use a function with a guard seems like it would be the most elegant solution.<br></blockquote></div><div><br>It would open a can of worms that I would rather see remain closed, specifically side effects and message passing. Just being able to declare something as "guard safe" wouldn't really help as the system would still have to be able to do something sensible in all cases. Otherswise you would be surprised what users can dream up. "<font size="2">It's hard to make a program foolproof because fools are so ingenious".<br>
<br></font></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Any suggestions?</blockquote><div><br>I used the following macros for just that case:<br>
<br></div></div><font size="2">%% {r,Left,Key,Val,Right}<br>
%% {b,Left,Key,Val,Right}<br>
%% empty<br>
<br>
-define(IS_RED(N), (is_tuple(N) andalso element(1, N) == r)).<br>
%% -define(IS_BLACK(N), not (is_tuple(N) andalso element(1, N) == r)).<br>
-define(IS_BLACK(N),<br>
((is_tuple(N) andalso (element(1, N) == b)) orelse (N == empty))).<br><br></font><font size="2">These are guard safe and usable on the rhs as well. If they are the most efficient I don't know.<br><br>I have also found another algorithm for deleting if you are interested which is a little faster. At least in my implementation. Deleting in RB-trees is not trivial.<br>
<br>Robert<br><br></font>
</blockquote></div><br>