<div dir="ltr">Well, this example could be easily written with Erlang _if_ operator:<div><div><br></div><div>muldiv(First, Second) -></div><div>    if</div><div>        not is_integer(First) -> {error, "First must be an integer"};</div><div>        not is_integer(Second) -> {error, "Second must be an integer"};</div><div>        Second =:= 0 ->  {error, "Second must not be zero!"};</div><div>        true -></div><div>            Quotient = First div Second,</div><div>            Product = First * Second,</div><div>            {ok, {Product, Quotient}}</div><div>    end.</div></div><div><br></div><div><br></div><div><div><div class="gmail_extra"><div><br></div><div class="gmail_quote">2017-09-26 9:40 GMT+07:00 <a href="mailto:dploop@163.com">dploop@163.com</a> <span dir="ltr"><<a href="mailto:dploop@163.com" target="_blank">dploop@163.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<div>This problem also <span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:1.5">bothered me for a long time, so I write a parse transform to deal with it. To be </span><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:1.5">specific, say we want to write a function which takes two integers and returns  the product and </span><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace">quotient of them.</font></span></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><br></font></span></div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">muldiv</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span>, <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span>
    <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">is_integer</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> ->
            <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">is_integer</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
                <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> ->
                    <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Product</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">*</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>,
                    <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=/=</span> <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
                        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> ->
                            <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Quotient</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">div</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>,
                            {<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">ok</span>, {<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Product</span>, <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Quotient</span>}};
                        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> ->
                            {<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">error</span>, <span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>Second must not be zero!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>}
                    <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>;
                <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> ->
                    {<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">error</span>, <span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>Second must be an integer!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>}
            <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>;
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> ->
            {<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">error</span>, <span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>First must be an integer!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>}
    <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>.</pre></div><div><br></div><div>Ugly, at least I think this "rocket" is not beautiful. Even worse, if requirement changed, you should modify this code on a large scale. To avoid this problem, you can just rewrite your code like this</div><div><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">muldiv</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span>, <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-></span>
    <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">do@</span>([<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">esugar_do_transform_error</span> ||
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">is_integer</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">return</span>(<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">next</span>);
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">fail</span>(<span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>First must be an integer!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>)
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>,
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">is_integer</span>(<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>) <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">return</span>(<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">next</span>);
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">fail</span>(<span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>Second must be an integer!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>)
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>,
        <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Product</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">*</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>,
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">case</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=/=</span> <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">0</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">of</span>
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">true</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">return</span>(<span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">next</span>);
        <span class="gmail-m_5476096270436700954pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">false</span> -> <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">fail</span>(<span class="gmail-m_5476096270436700954pl-s" style="box-sizing:border-box;color:rgb(3,47,98)"><span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span>Second must not be zero!<span class="gmail-m_5476096270436700954pl-pds" style="box-sizing:border-box">"</span></span>)
        <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">end</span>,
        <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Quotient</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">=</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">First</span> <span class="gmail-m_5476096270436700954pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">div</span> <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Second</span>,
        <span class="gmail-m_5476096270436700954pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">return</span>({<span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Product</span>, <span class="gmail-m_5476096270436700954pl-smi" style="box-sizing:border-box">Quotient</span>})
    ]).</pre></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><br></font></span></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace">This idea came from the Haskell's do grammer, and to use this grammer, you just need to write a error monad.</font></span></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><br></font></span></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace">This parse transform code is one module of my esugar(</font></span><span style="font-family:"SFMono-Regular, Consolas, ";font-size:10.5pt;line-height:1.5;background-color:window">An Erlang Syntactic <wbr>Sugar Library</span><span style="color:rgb(36,41,46);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px">) library, and the address is (</span><span style="font-family:"SFMono-Regular, Consolas, ";font-size:10.5pt;line-height:1.5;background-color:window"></span><a href="https://github.com/dploop/esugar)" style="font-family:"";font-size:10.5pt;line-height:1.5;background-color:window" target="_blank">https://github.com/dploop/<wbr>esugar)</a><span style="font-family:"";font-size:10.5pt;line-height:1.5;background-color:window"> </span></div><span></span><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><br></font></span></div><div><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><span style="line-height:19px">I'm glad to discuss with you about how to use it or improve it(because I just write this toy for fun at the begining).</span></font></div><div><span style="background-color:rgba(0,0,0,0);font-size:10.5pt;line-height:19px"><font color="#24292e" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><br></font></span></div>
<div><br></div><hr style="width:210px;height:1px" color="#b5c4df" size="1" align="left">
<div><span><div style="margin:10px;font-family:verdana;font-size:10pt"><div><a href="mailto:dploop@163.com" target="_blank">dploop@163.com</a></div></div></span></div>
<blockquote style="margin-top:0px;margin-bottom:0px;margin-left:0.5em"><div> </div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm"><div style="padding:8px;font-size:12px;font-family:tahoma;color:rgb(0,0,0);background:rgb(239,239,239)"><div><b>From:</b> <a href="mailto:codewiget95@gmail.com" target="_blank">code wiget</a></div><div><b>Date:</b> 2017-09-25 21:25</div><div><b>To:</b> <a href="mailto:erlang-questions@erlang.org" target="_blank">Erlang-Questions Questions</a></div><div><b>Subject:</b> [erlang-questions] Nested Case Statements v.s. multiple functions</div></div></div><div><div class="gmail-h5"><div><div>Hello everyone,</div>
<div> </div>
<div>As I get further into Erlang, I am starting to realize that some of my functions have been getting pretty ugly with nested case statements. For example, I had a nested case statement that looked something like this:</div>
<div> </div>
<div>Send_update(Arg1) -></div>
<div>     case do this(Arg1) of</div>
<div>             {ok, [Val1, Val2]} ->  </div>
<div>                     case do_that(Val1, Val2) of</div>
<div>                             {ok, [Val3, Val4]} -></div>
<div>                                     case do_this2(…) of </div>
<div>….</div>
<div> </div>
<div>It continued into this for another few functions, you get the picture - its ugly, and it is hard to read. </div>
<div> </div>
<div>So I went and I converted it to a top level function that would then call lower level functions like so:</div>
<div>                     </div>
<div> </div>
<div> </div>
<div> send_update(Arg1) ->  </div>
<div>     case ... of</div>
<div>         {ok, [Val1, Val2]} ->  </div>
<div>             send_update(check_indices, {Arg1, Val1, Val2});</div>
<div>         Else ->  </div>
<div>             lager:error("ERROR: ..")</div>
<div>     end.</div>
<div> send_update(check_indices, {Arg1, Arg2, Arg3}) -></div>
<div>     case check_indices(Arg2, Arg3)of </div>
<div>         true -></div>
<div>             send_update(get_values, {Arg1, Arg3});</div>
<div>         false -></div>
<div>             lager:error("EMERGENCY: ….")</div>
<div>     end;</div>
<div> send_update(get_values, {Arg1, Arg2}) -></div>
<div>   ...</div>
<div>     case ... of  </div>
<div>         {ok, [Val1, Val2, VAl3]} -></div>
<div>             send_update(send_value, {Arg1, Val1, Val2, Val3});</div>
<div>         Error ->  </div>
<div>             lager:error("ERROR: …")</div>
<div>     end;</div>
<div> send_update(send_value, {Arg1, Arg2, Arg3, Arg4}) -></div>
<div>    …</div>
<div>     Do_something(Args),</div>
<div>     ok. </div>
<div> </div>
<div> </div>
<div>Now that I look at it though, both don’t look right. They don’t look like something I would write in any other language where I would just have if’s and else’s.</div>
<div> </div>
<div>Is this the proper way to write Erlang? I know everyone has their own style, but I assume there is some accepted form of writing functional programs with deep nests.</div>
<div> </div>
<div>Thank you for your advice!</div>
<div>______________________________<wbr>_________________</div>
<div>erlang-questions mailing list</div>
<div><a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a></div>
<div><a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a></div>
</div></div></div></blockquote>
</div><br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div></div></div>