if statement - Erlang equivalent to if else -
i have 2 parts of code want execute. both conditionals
if value1 < n else if value1 >= n if value2 < n else if value2 >= n
i want @ 1 statement of each execute.
how if work in erlang? there no else. use multiple guards, looks have 4 if statements. in groups of 2.
if condition code; if other condition code end.
i syntax error.
the form if
is:
if <guard 1> -> <body1> ; <guard 2> -> <body2> ; ... end
it works trying guards in if-clauses in top-down order (this defined) until reaches test succeeds, body of clause evaluated , if
expression returns value of last expression in body. else
bit in other languages baked it. if none of guards succeeds if_clause
error generated. common catch-all guard true
succeeds, catch-all can true.
the form case
is:
case <expr> of <pat 1> -> <body1> ; <pat 2> -> <body2> ; ... end
it works first evaluating , trying match value patterns in case-clauses in op-down order (this defined) until 1 matches, body of clause evaluated , case
expression returns value last expression in body. if no pattern matches case_clause
error generated.
note if
, case
both expressions (everything expression) both must return values. 1 reason why there no default value if nothing succeeds/matches. force cover options; important case
. if
degenerate case of case
inherited it. there bit of history of if
in erlang rationale can find on trapexit.org under user contributions.
Comments
Post a Comment