[erlang-questions] Compound guard expression in case

Hugo Mills hugo@REDACTED
Sun Feb 10 20:43:29 CET 2019


On Sun, Feb 10, 2019 at 02:33:36PM -0500, Donald Steven wrote:
> Newbie question regarding best practices (including a string of nested ifs):
> 
> How do you code the following:

   Guards:

> case X of
>     4             -> something;
>     <2 or >6 -> something else;
>     _             -> something else again
> end,

case X of
    4 ->
        something;
    Y when Y < 2; Y > 6 ->
        something else;
    _ ->
        something else again
end,

Note that >=2 and =<6 would be written with "when Y >= 2, Y =< 6" --
use comma for "and", semicolon for "or", with "or" being at the higher
level:

   Y when Y >= 2, Y =< 6; Y >= 8, Y =< 10 ->
       Y is either between 2 and 6 or between 8 and 10 inclusive;

> and
> 
> case X of
>     atom1 or atom2 or atom3 -> something;
>     atom4                                    -> something else;
>     _ ->                                         -> something else again
> end,

case X of
    Y when Y =:= atom1; Y =:= atom2; Y =:= atom3 ->
        something;
    atom4 ->
        something else;
    _ ->
        something else again
end,

   Hugo.

-- 
Hugo Mills             | I have a step-ladder. My real ladder left when I was
hugo@REDACTED carfax.org.uk | a child.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190210/f8369a6a/attachment.bin>


More information about the erlang-questions mailing list