<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7226.0">
<TITLE>RE: newbie question</TITLE>
</HEAD>
<BODY>
<DIV id=idOWAReplyText83214 dir=ltr>
<DIV dir=ltr><FONT face="Courier New" color=#000000 size=2>One significant
difference when it comes to side-effects is</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>that in 'case' you will *always*
pass through that single "thing"</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>causing a side effect. In ifs, in
contrast, you may execute one</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>or several if-clauses depending on
if and when an if-clause</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>turns true. Making the latter much
more difficult to follow.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Best Regards</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Lennart</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>case
function_having_side_effects(X) of</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2> foo -></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>
....;</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2> bar -></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>
....</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>end,</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>if</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>
one_function_having_side_effect(X)==foo -></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New"
size=2> ....</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>
another_function_with_side_effects(X)/=dog -></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New"
size=2> ....</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>
even_more_sideeffects(X)>10 -></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New"
size=2> ....;</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2> true
-></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New"
size=2>
i_have_done_all_side_effects</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>end.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" color=#000000
size=2></FONT> </DIV></DIV>
<DIV id=idSignature96313 dir=ltr>
<DIV><FONT face="Courier New" color=#000000
size=2>-------------------------------------------------------------</FONT></DIV>
<DIV><FONT face="Courier New" color=#000000 size=2>Lennart
Ohman
phone : +46-8-587 623 27</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sjöland & Thyselius Telecom
AB cellular: +46-70-552 6735</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sehlstedtsgatan
6
fax : +46-8-667 8230</FONT></DIV>
<DIV><FONT face="Courier New" size=2>SE-115 28 STOCKHOLM,
SWEDEN email : <A
href="mailto:lennart.ohman@st.se">lennart.ohman@st.se</A></FONT></DIV></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>Från:</B> owner-erlang-questions@erlang.org genom
Thomas Johnsson XA (LN/EAB)<BR><B>Skickat:</B> ti 2005-05-17
13:37<BR><B>Till:</B> erlang-questions@erlang.org<BR><B>Ämne:</B> RE: newbie
question<BR></FONT><BR></DIV>
<DIV>
<P><FONT size=2>There are situations where it is important that guards are fast
and side effect-free (in receive, for example).<BR>Conditions in if-expressions
is *not* one of those places, IMHO. (Except for the usual good reasons to<BR>to
program as 'functionally' as possible).<BR>Ie, there is no reason that I can see
why the if-condition has to be a guard syntactically rather than an
expression.<BR>I just forces the programmer to write something more
clumsy.<BR><BR>Or is there some light I haven't yet seen?<BR>--
Thomas<BR><BR>-----Original Message-----<BR>From:
owner-erlang-questions@erlang.org<BR>[<A
href="mailto:owner-erlang-questions@erlang.org">mailto:owner-erlang-questions@erlang.org</A>]On
Behalf Of Matthias Lang<BR>Sent: den 17 maj 2005 13:20<BR>To: Dietmar
Schaefer<BR>Cc: erlang-questions@erlang.org<BR>Subject: Re: newbie
question<BR><BR><BR>From the FAQ:<BR><BR> | 9.4. Why can't I call
arbitrary functions in a guard?<BR> |<BR> | If that was allowed,
there would be no guarantee that guards were side-effect free.<BR>
|<BR> | Also, it is convenient to be able to program as though guards do
not consume any significant amount of execution time. There's a list of BIFs
which can be called from within guards in the Erlang book and the standard
Erlang spec, some examples are size(), length(), integer(), record().<BR>
|<BR> | The "problem" often crops up when using if:<BR> |<BR>
| issue_warning() -><BR> |
if (os:type() == {win32, windows}) -> %% illegal
guard<BR> | ok = io:fwrite("this
computer may crash at any time\n");<BR> | true
-><BR> | ok = io:fwrite("no
problem\n")<BR> | end.<BR> |
<BR> |<BR> | The solution is usually to use case
instead. Case is used much more frequently than if in most Erlang
programs:<BR> |<BR> | issue_warning()
-><BR> |
case os:type() of<BR>
|
{win32, windows} -> ok = io:fwrite("crash soon\n");<BR>
| _
-> ok = io:fwrite("no problem\n")<BR> |
end.<BR> | <BR><BR>Matthias<BR><BR>Dietmar Schaefer
writes:<BR> > Hi !<BR> ><BR> > Isn't that supposed to
work ?<BR> ><BR> ><BR> > if
cmmc_db:getProcState(Key,Name) /= State -><BR> ><BR> >
instead I have to write<BR> ><BR> > ProcState =
cmmc_db:getProcState(Key,Name),<BR> ><BR> > if ProcState /=
State -><BR> ><BR> ><BR> ><BR> >
regards<BR> ><BR> ><BR> >
Dietmar<BR></FONT></P></DIV>
</BODY>
</HTML>