<div><span style="line-height: 1.5; font-size: 14px;">Aha!Gotcha! </span></div><div><span style="line-height: 1.5; font-size: 14px;">It is a good way to flat a multiple nested.It's truly not need to write 8 </span><span style="font-size: 14px; line-height: 1.5;">branches</span></div><div><span style="line-height: 1.5; font-size: 14px;"> example(Foo, Bar, Ugh) -></span></div><div><div>     case {Foo, Bar, Ugh}<br>      of {false, _, _} -> . . .<br>      ;  {true, false, _} -> . . .<br>      ;  {true, true, false} -> . . .<br>      ;  {true, true, true} -> . . .<br>     end. </div><div><br></div><div>Use if could be better I think,like:</div><div>example(Foo,Bar,Ugh) -></div><div>if</div><div>    Foo =:= false -> ...</div><div>    Bar =:= <span style="line-height: 1.5; font-size: 14px;">false</span><span style="line-height: 1.5; font-size: 14px;"> </span><span style="line-height: 1.5; font-size: 14px;"> -> ...</span></div><div>    Ugh<span style="line-height: 1.5; font-size: 14px;"> </span><span style="line-height: 1.5; font-size: 14px;">=:= </span><span style="line-height: 1.5; font-size: 14px;">false</span><span style="line-height: 1.5; font-size: 14px;"> -> ...</span></div><div>    true<span style="line-height: 1.5; font-size: 14px;"> -> ...</span></div><div>(change some logic could remove the "=:= false", to make the code more clean)</div><div><br></div><div>And this is the better way I thing. (I just going to refactor my code like  this)</div><div>    index_of_subtuple(Whole, Part)<br>      when is_tuple(Whole), is_tuple(Part), tuple_size(Whole) >= tuple_size(Part) -><br>        . . . straightforward case . . .;<br>    index_of_subtuple(Whole, Part)<br>      when is_tuple(Whole), is_tuple(Part) -><br>        0.</div><div><br></div><div>Though I still not agree with that, "!Something" is always better than "Something == false".</div><div>Because I still consider that "<span style="line-height: 1.5; font-size: 14px;">Something == false" could more easy to read for the people like me ....</span></div><div><br></div><div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">------------------ 原始邮件 ------------------</div><div style="font-size: 12px;background:#efefef;padding:8px;"><div><b>发件人:</b> "Richard A. O&#39;Keefe"<ok@cs.otago.ac.nz>;</div><div><b>发送时间:</b> 2013年3月26日(星期二) 下午2:14</div><div><b>收件人:</b> "饕餮"<249505968@qq.com>; <wbr></div><div><b>抄送:</b> "erlang-questions"<erlang-questions@erlang.org>; <wbr></div><div><b>主题:</b> Re: 回复: [erlang-questions] How to make a short circuit in erlang ?</div></div><div><br></div><br>On 26/03/2013, at 4:24 PM, 饕餮 wrote:<br><br>> hmmmmm<br>> Something I just don't agree.<br>> I use Something == false.<br><br>This is, I repeat, Bad Style in ANY Programming Language.<br><br>It has been bad style ever since Algol got the Boolean type in<br>1960 and Fortran got the LOGICAL type in 1961.<br><br>> Because I do not name the variable like Is_Something_Right (Apologize for my mike).<br><br>Why do you think that a bad choice of names justifies inappropriate comparison?<br><br>> !Is_Something_Right could be a better code style.<br>> !Something , in my opinion, is not better than Something == false.<br><br>It is, if Something is well named.<br><br>The important thing here is that Boolean values are part of a Boolean<br>_algebra_, and by avoiding the natural operations of that algebra,<br>you are making combinations of Booleans __hard to think__ when they<br>need not be.<br><br>I must point out that having Boolean-valued variables _at all_ in Erlang<br>is definitely odd.  It's possible.  It's not _wrong_.  But it _is_ odd,<br>and there is usually a more idiomatic way to do it.<br>> <br>> I have see this code style in my project as you write.<br>> example(Foo, Bar, Ugh) -><br>>     case {Foo, Bar, Ugh}<br>>       of {false, _, _} -> . . .<br>>        ; {true, false, _} -> . . .<br>>        ; {true, true, false} -> . . .<br>>        ; {true, true, true} -> . . .<br>>     end. <br>> But when look at the {true,true,false}.<br>> I still need to check which one is true , and which is false (I'm just stupid as a duck).<br><br>When you look at the {true,true,false} case, you can *see* which one<br>is false (Ugh) and which are not (Foo, Bar).  If you can't, you have<br>too much code in one place.<br><br>Of course, something like this should be encoded using one expression<br>with multiple values rather than several Booleans. <br><br>> And There could be 8 different status.<br>> That means I should write 8 entry for 3 judgement<br><br>No, for three judgements you need three entries.<br><br>Let's switch to Haskell for the moment.<br><br>Let t1, t2, t3 be three arbitrary expressions of type Bool<br>and e1, e2, e3, e4 be four arbitrary expressions of a<br>common type alpha.<br><br>Then<br>  if t1 then e1 else<br>    if t2 then e2 else<br>    if t3 then e3 else t4<br>and<br>    case (t1,t2,t3) of<br>      (True, _,    _)     -> e1<br>          (False,True, _)     -> e2  <br>          (False,False,True)  -> e3<br>        (False,False,False) -> e4<br><br>are equivalent.  In fact, a good Haskell compiler will probably generate the<br>same code for both of them.<br><br>Both examples make FOUR judgements based on THREE variables.<br><br>In Erlang, they are only equivalent in the absence of exceptions and<br>side effects.<br><br>But the scheme as outlined does *NOT* have 2^n branches; it has<br>*exactly* the same number of branches as an 'if' nest would have.<br>So your argument for disliking it is unsound.<br><br>> I just wonder is there any way like return in other language could just return and ignore the step below.<br><br>Many other programming languages do _not_ have a 'return e;' statement.<br>In particular, functional languages such as ML, Clean, Haskell, and F#<br>don't.  From a web page about F#:<br><br>      One of the limitations of F# is that it doesn't very well<br>     support some of the advanced imperative language constructs<br>   such as break, continue or imperative style of returning<br>      value from a function, meaning that you can't write something<br> like return false in the middle of the function.  This has<br>       good reasons.<br><br>> And Now I know there are no way to do like that.<br><br>A real example of something you need to write would be really really useful.<br><br>Here's a little example.<br>We are given two tuples Whole and Part and want to find<br>the smallest integer I such that Whole[I..I+tuple_size(Part)-1] = Part.<br>We want to return 0 if there is no such I.  We might begin<br><br>    index_of_subtuple(Whole, Part)<br>      when is_tuple(Whole), is_tuple(Part), tuple_size(Whole) >= tuple_size(Part) -><br>        . . . straightforward case . . .;<br>    index_of_subtuple(Whole, Part)<br>      when is_tuple(Whole), is_tuple(Part) -><br>        0.<br><br>You are probably used to the idea that a function can have only one body,<br>which is not true in Erlang (or Haskell, or ML, or Clean, or F#, . . .).<br><br></div>