[erlang-questions] Compound guard expression in case

Donald Steven t6sn7gt@REDACTED
Mon Feb 11 01:43:01 CET 2019


Thanks, re the first example.  I had an if statement solution, but I was 
under the impression that ifs were frowned upon.

Re the second example, each atom in fact represents a possible musical 
fork.  So, to create a bit of an odd but useful example:

case SoundType of:

     ostinato                                             -> some action;
     melisma or melody or ornament  -> some action common to all three 
atoms;
    _                                                           -> 
error_message()    % i.e., an unrecognized SoundType
end,

I was trying to understand how to do the 'or' part (the second guard 
statement) without repeating the code 3 times.

On 2/10/2019 7.17 PM, Richard O'Keefe wrote:
> The first example would probably be better as
>    if X =:= 4 -> first case
>     ; X >= 2, X =< 6 -> third case
>     ; true  -> second case
>    end
>
> The second example makes me unhappy.  We are so far in the misty clouds of
> abstraction here that we can't see the ground with a telescope.  And the
> thing that makes me unhappy is the "_" case.  This is a maintenance bug
> waiting to happen, and it does not matter what your programming 
> language is.
> This is the ML lesson.  A concrete example would be a big help.
>
> On Mon, 11 Feb 2019 at 08:43, Hugo Mills <hugo@REDACTED 
> <mailto:hugo@REDACTED>> wrote:
>
>     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 <http://carfax.org.uk> | a child.
>     http://carfax.org.uk/ |
>     PGP: E2AB1DE4          |
>     _______________________________________________
>     erlang-questions mailing list
>     erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
>     http://erlang.org/mailman/listinfo/erlang-questions
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190210/7085f202/attachment.htm>


More information about the erlang-questions mailing list