[erlang-questions] (newbie) Using Functions as Guards in Erlang

Jachym Holecek <>
Fri Jun 13 13:19:42 CEST 2008


On Fri, 13 Jun 2008 11:43:54 +0200, Fuad Tabba <>  
wrote:
> Being able to use a function with a guard seems like it would be the most
> elegant solution.

It could be useful and I admit I don't understand why is that prohibited  
(besides
"side effects and infinite loops in guards are Evil" to which one can  
reply "the
compiler can refuse a guard function if it can't prove it to be safe").  
Anyway,
I'm not a compiler expert, so maybe there's a good reason I'm missing.

> Any suggestions?

Maybe choose different data representation that allows you to pattern-match
node colour? Something along the lines of:

   -record(rb_node, {
       left,      %% Left subtree.  :: rb_node()
       right,     %% Right subtree. :: rb_node()
       is_black,  %% Red or black?  :: bool()
       data}).    %% Contents.      :: term()

Either that, or give up doing all the dispatch in function clauses; ie.  
live
with having explicit 'case' statements (there's nothing wrong about them  
after
all). Or restructure the code in different way, but it's hard to suggest  
how
w/o seeing the rest of the module...

HTH,
	-- Jachym




More information about the erlang-questions mailing list