<div class="gmail_quote">I saw something like this in some Erlang code. It is possible to achieve a similar effect without using "if" as follows:<br><br>is_list(X) andalso string:to_lower(X) == X andalso length(X) == 5 and also my:no_duplicates(X) andalso do_something(X).<br>

<br>do_something(X) -><br>    %% Stuff...<br>    true.<br><br>Note the use of "==" instead of "=". Huge difference.<br><br>This is similar to Unix-ish shell-scripting techniques such as [[ -z "$NAME" ]] && echo "NAME not defined"<br>
<font color="#888888">
<br>Edwin</font><div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Thu, May 15, 2008 at 2:10 PM, Darren New <<a href="mailto:dnew@san.rr.com" target="_blank">dnew@san.rr.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;">

Being a newbie to Erlang, I wonder if there's a reason for the "if"<br>
statement to disallow side-effects in the guards, or whether it's just a<br>
result of using the same language construct for "if" as for "receive".<br>
I.e., I understand why guards in "receive" can't have side-effects, but<br>
the guards in "if" don't really seem to have that same problem.<br>
<br>
I often find myself often stumbling over that fact, needing to write a<br>
nested-three-deep "if" or auxiliary functions to do relatively simple<br>
(and side-effect-free operations) simply because the tests are too<br>
complex (calling my own code). For example, I can't say<br>
<br>
   if X is a list, and the list is length five,<br>
      and there are no duplicates -> good;<br>
<br>
I have to<br>
   if X is a list -><br>
      Y = string:tolower(X),<br>
      if X = Y -><br>
        if X is length five -><br>
          V = my:no_duplicates(X),<br>
            if V -> good;<br>
<br>
Rather than the more straightforward<br>
   if X is a list andalso string:tolower(X) = X<br>
     andalso len(X) = 5 andalso true = my:noduplicates(X) -> good;<br>
<br>
Is there a reason for this other than not having to define (and<br>
implement) two different kinds of guards?<br>
<br>
Or am I doing something suboptimal in my programming by doing this, and<br>
there's really a clever but unobvious-to-me way of making this work better?<br>
<br></blockquote></div></div></div></div><br>