<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">On 09/25/2017 07:40 PM, <a class="moz-txt-link-abbreviated" href="mailto:dploop@163.com">dploop@163.com</a>
      wrote:<br>
    </div>
    <blockquote cite="mid:2017092610402738729017@163.com" type="cite">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <style>body { line-height: 1.5; }blockquote { margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em; }body { font-size: 10.5pt; font-family: 微软雅黑; color: rgb(0, 0, 0); line-height: 1.5; }</style>
      <div style="orphans: 2; widows: 2; ">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 face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e">quotient
            of them.</font></span></div>
      <div style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e"><br>
          </font></span></div>
      <div style="orphans: 2; widows: 2; ">
        <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-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);"><span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">muldiv</span>(<span class="pl-smi" style="box-sizing: border-box;">First</span>, <span class="pl-smi" style="box-sizing: border-box;">Second</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-></span>
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">is_integer</span>(<span class="pl-smi" style="box-sizing: border-box;">First</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> ->
            <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">is_integer</span>(<span class="pl-smi" style="box-sizing: border-box;">Second</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
                <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> ->
                    <span class="pl-smi" style="box-sizing: border-box;">Product</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-smi" style="box-sizing: border-box;">First</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">*</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span>,
                    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=/=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">0</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
                        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> ->
                            <span class="pl-smi" style="box-sizing: border-box;">Quotient</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-smi" style="box-sizing: border-box;">First</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">div</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span>,
                            {<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">ok</span>, {<span class="pl-smi" style="box-sizing: border-box;">Product</span>, <span class="pl-smi" style="box-sizing: border-box;">Quotient</span>}};
                        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> ->
                            {<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">error</span>, <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Second must not be zero!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>}
                    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>;
                <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> ->
                    {<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">error</span>, <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Second must be an integer!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>}
            <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>;
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> ->
            {<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">error</span>, <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>First must be an integer!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>}
    <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>.</pre>
      </div>
      <div style="orphans: 2; widows: 2; "><br>
      </div>
    </blockquote>
    Obsessing over a rocket shape doesn't seem as important as the
    absence of any guard expression usage here.  Guards are
    unfortunately often ignored by Erlang programmers, though they are
    the one thing in Erlang without side-effects.  Ideally, the source
    code is written to break when bad input is provided, and that
    doesn't require that you define the exception thrown for bad input. 
    A more minimal example without the added complexity here is below:<br>
    <br>
    muldiv(First, 0)<br>
        when is_integer(First) -><br>
        0;<br>
    muldiv(First, Second)<br>
        when is_integer(First), is_integer(Second) -><br>
        {ok, {First * Second, First div Second}}.<br>
    <br>
    That gets rid of all the noise and useless error returns, and
    instead lets the process die when there is a problem, unless the
    exception is caught and handled.  You could add another function
    clause to do an error/exit/throw on bad input, so it is a clear
    misuse of a function which would be checked with dialyzer, and
    dialyzer should be capable of inferring the types from the guards
    (the current code above should be known to only accept integers, and
    other usage would cause a dialyzer error... adding a bad input
    function clause would break that, but then that could be fixed by
    the Erlang type specification), even if the Erlang type
    specification isn't added.<br>
    <br>
    Best Regards,<br>
    Michael<br>
    <br>
    <blockquote cite="mid:2017092610402738729017@163.com" type="cite">
      <div style="orphans: 2; widows: 2; ">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 style="orphans: 2; widows: 2; ">
        <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-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);"><span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">muldiv</span>(<span class="pl-smi" style="box-sizing: border-box;">First</span>, <span class="pl-smi" style="box-sizing: border-box;">Second</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">-></span>
    <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">do@</span>([<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">esugar_do_transform_error</span> ||
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">is_integer</span>(<span class="pl-smi" style="box-sizing: border-box;">First</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">return</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">next</span>);
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">fail</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>First must be an integer!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>,
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">is_integer</span>(<span class="pl-smi" style="box-sizing: border-box;">Second</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">return</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">next</span>);
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">fail</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Second must be an integer!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>,
        <span class="pl-smi" style="box-sizing: border-box;">Product</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-smi" style="box-sizing: border-box;">First</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">*</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span>,
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">case</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=/=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">0</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">of</span>
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">true</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">return</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">next</span>);
        <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">false</span> -> <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">fail</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Second must not be zero!<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
        <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">end</span>,
        <span class="pl-smi" style="box-sizing: border-box;">Quotient</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-smi" style="box-sizing: border-box;">First</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">div</span> <span class="pl-smi" style="box-sizing: border-box;">Second</span>,
        <span class="pl-en" style="box-sizing: border-box; color: rgb(111, 66, 193);">return</span>({<span class="pl-smi" style="box-sizing: border-box;">Product</span>, <span class="pl-smi" style="box-sizing: border-box;">Quotient</span>})
    ]).</pre>
      </div>
      <div style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e"><br>
          </font></span></div>
      <div style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e">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 style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e"><br>
          </font></span></div>
      <div style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e">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 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
          moz-do-not-send="true"
          href="https://github.com/dploop/esugar%29" sfmono-regular,=""
          consolas,="" '";="" font-size:="" 14px;="" font-weight:=""
          normal;="" font-style:="" normal;'="" style="font-family: '';
          font-size: 10.5pt; line-height: 1.5; background-color:
          window;" class=""><a class="moz-txt-link-freetext" href="https://github.com/dploop/esugar">https://github.com/dploop/esugar</a>)</a><span
          sfmono-regular,="" consolas,="" '";="" font-size:="" 14px;=""
          color:="" rgb(0,="" 0,="" 0);="" background-color:=""
          rgb(255,="" 255,="" 255);="" font-weight:="" normal;=""
          font-style:="" normal;text-decoration:="" none;'=""
          style="font-family: ''; font-size: 10.5pt; line-height: 1.5;
          background-color: window;"> </span></div>
      <span style="font-family: "" sfmono-regular,="" consolas,=""
        '";="" font-size:="" 14px;="" color:="" rgb(0,="" 0,="" 0);=""
        background-color:="" rgb(255,="" 255,="" 255);=""
        font-weight:="" normal;="" font-style:=""
        normal;text-decoration:="" none;'=""></span>
      <div style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e"><br>
          </font></span></div>
      <div style="orphans: 2; widows: 2; "><font face="SFMono-Regular,
          Consolas, Liberation Mono, Menlo, Courier, monospace"
          color="#24292e"><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 style="orphans: 2; widows: 2; "><span
          style="background-color: rgba(0, 0, 0, 0); font-size: 10.5pt;
          line-height: 19px;"><font face="SFMono-Regular, Consolas,
            Liberation Mono, Menlo, Courier, monospace" color="#24292e"><br>
          </font></span></div>
      <div><br>
      </div>
      <hr style="width: 210px; height: 1px;" size="1" color="#b5c4df"
        align="left">
      <div><span>
          <div style="MARGIN: 10px; FONT-FAMILY: verdana; FONT-SIZE:
            10pt">
            <div><a class="moz-txt-link-abbreviated" href="mailto:dploop@163.com">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:none;border-top:solid #B5C4DF
          1.0pt;padding:3.0pt 0cm 0cm 0cm">
          <div style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; FONT-SIZE:
            12px;FONT-FAMILY:tahoma;COLOR:#000000; BACKGROUND: #efefef;
            PADDING-BOTTOM: 8px; PADDING-TOP: 8px">
            <div><b>From:</b> <a moz-do-not-send="true"
                href="mailto:codewiget95@gmail.com">code wiget</a></div>
            <div><b>Date:</b> 2017-09-25 21:25</div>
            <div><b>To:</b> <a moz-do-not-send="true"
                href="mailto:erlang-questions@erlang.org">Erlang-Questions
                Questions</a></div>
            <div><b>Subject:</b> [erlang-questions] Nested Case
              Statements v.s. multiple functions</div>
          </div>
        </div>
        <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>_______________________________________________</div>
          <div>erlang-questions mailing list</div>
          <div><a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a></div>
          <div><a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a></div>
        </div>
      </blockquote>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
erlang-questions mailing list
<a class="moz-txt-link-abbreviated" href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>
<a class="moz-txt-link-freetext" href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>