Erlang equivalent to if else

前端 未结 4 622
傲寒
傲寒 2020-12-16 14:59

I have 2 parts of code I want to execute. Both are conditionals

if Value1 < N do something 

else if Value1 >= N do something

if Value2 < N do some         


        
相关标签:
4条回答
  • 2020-12-16 15:26

    First of all, I recommend you to get used to use 'case' statement, because of 'if' conditions restricted to guard expressions:

    case custom_call(A) of
      1 -> do1(A);
      2 -> do2(A)
    end.
    

    There is one more way to do conditional execution besides 'if' and 'case' that works starting from R13:

      1> N =10.
      10
      2> ((N > 10) andalso more).      
      false
      3> ((N == 10) andalso equals).
      equals
    
    0 讨论(0)
  • 2020-12-16 15:32

    The form for an if is:

    if
        <guard 1> -> <body1> ;
        <guard 2> -> <body2> ;
        ...
    end
    

    It works trying the guards in if-clauses in top-down order (this is defined) until it reaches a test which succeeds, then the body of that clause is evaluated and the if expression returns the value of the last expression in the body. So the else bit in other languages is baked into it. If none of the guards succeeds then an if_clause error is generated. A common catch-all guard is just true which always succeeds, but a catch-all can be anything which is true.

    The form for a case is:

    case <expr> of
        <pat 1> -> <body1> ;
        <pat 2> -> <body2> ;
        ...
    end
    

    It works by first evaluating and then trying to match that value with patterns in the case-clauses in op-down order (this is defined) until one matches, then the body of that clause is evaluated and the case expression returns the value last expression in the body. If no pattern matches then a case_clause error is generated.

    Note that if and case are both expressions (everything is an expression) so they both must return values. That is one reason why there is no default value if nothing succeeds/matches. Also to force you to cover all options; this is especially important for case. if is just a degenerate case of case so it inherited it. There is a bit of history of if in the Erlang Rationale which you can find on trapexit.org under user contributions.

    0 讨论(0)
  • 2020-12-16 15:43

    Erlang doesn't allow you to have an if without a true statement option. Whether or not this is something that is a true statement or an actual true is up to you, but it is commonplace to have your true be the else in other languages.

    if 
        some_condition -> some_code;
        some_other_condition -> some_other_code;
        true -> else_code
    end.
    

    See the "What the If?" section on this page for more on this.

    0 讨论(0)
  • 2020-12-16 15:46

    Remember if in Erlang has a value to return, and it's an expression. It's not that if like in C or Java.

    If you want to do something for a value, the code should be something like this;

    if
      % do something and get the value
      X >= Val -> Something;
      % for doing something otherwise and get the value
      true -> Else_than_the_Something 
    end.
    

    See Section for the if expression of Erlang Reference Manual for the further details.

    0 讨论(0)
提交回复
热议问题