Compound guard expression in case

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Compound guard expression in case

Donald Steven
Newbie question regarding best practices (including a string of nested ifs):

How do you code the following:

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

and

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

Thanks!

Don

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Compound guard expression in case

Hugo Mills-2
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@... carfax.org.uk | a child.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Compound guard expression in case

Richard O'Keefe
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 <[hidden email]> 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@... carfax.org.uk | a child.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Compound guard expression in case

Donald Steven
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 <[hidden email]> 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@... carfax.org.uk | a child.
http://carfax.org.uk/  |
PGP: E2AB1DE4          |
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions