[erlang-questions] Guards syntax for multiple values
Richard O'Keefe
raoknz@REDACTED
Wed Mar 27 12:25:52 CET 2019
I knew it! I knew it!
"½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒”
That list is NOT an exhaustive list of all the "VULGAR FRACTION n dTHS"
characters!
It was not even complete back in Unicode 6.3.
Nor is the version with two integer ranges complete.
If this list is not *intended* to include all such characters,
there had better be a very clear and explicit comment saying that it is
so and why it is so.
Let me put it this way: if the number of cases you want to cover this way
is sufficiently large that writing it is enough of a pain to comment on,
considering all the other things you have to do, you almost surely have the
list wrong. So I was *expecting* the list to miss something, and it was.
In fact the list is incomplete in two different ways.
First, it does not contain all the "VULGAR FRACTION *" characters.
Second, it does not contain *any* of the "ORIYA FRACTION *",
"TELUGU FRACTION *", "MALAYALAM FRACTION *", "NORTH INDIC FRACTION *",
"RUMI FRACTION *", or "COPTIC FRACTION *" characters (and there were
27 of those in Unicode 6.3), and there is no explanation of why not.
Seriously, by the time the function is adequately documented as to
which characters are included, which excluded, and why, the actual
code is relatively small.
On Tue, 26 Mar 2019 at 02:13, Florent Gallaire <fgallaire@REDACTED> wrote:
> Hello Brujo,
>
> Thanks for your answer.
>
> > Why not just…
> >
> > is_fraction(X) -> lists:member(X, "½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒”).
>
> Because it's not possible: "illegal guard expression".
>
> > Or, if you really really want to use function clause heads,
> pattern-matching and guards:
> >
> > is_fraction(X) when $¼ =< X =< $¾ -> true;
> > is_fraction(X) when $⅐ =< X <= $⅞ -> true;
> > is_fraction(_) -> false.
> >
> > For these kinds of character manipulation things, using the fact that
> they’re just integers under-the-hood is not a bad idea.
>
> Yes you're right in this case, but it remains a trick so most of the
> time it's not applicable.
>
> Cheers
>
> > Cheers!
> >
> > ________________________________
> > Brujo Benavides
> >
> >
> >
> > On 25 Mar 2019, at 09:38, Florent Gallaire <fgallaire@REDACTED> wrote:
> >
> > Hello Richard,
> >
> > Thanks for your answer.
> >
> > lists:member(X, [X1,X2,X3,X4]) answers true or false.
> > There is no fundamental reason that the compiler could not
> > expand that in-line to (X =:= X1 orselse ... orelse X =:= X4)
> > when the shape of the list is known. So we *definitely* need
> > no new syntax.
> >
> >
> > So if there's no reason the compiler could not do it, we *really*
> > should have a new syntax.
> >
> > We really need an actual concrete example of real code to discuss.
> >
> >
> > The developed version of the is_fraction/1 function:
> >
> > is_fraction($½) -> true;
> > is_fraction($⅓) -> true;
> > is_fraction($⅔) -> true;
> > is_fraction($¼) -> true;
> > is_fraction($¾) -> true;
> > is_fraction($⅕) -> true;
> > is_fraction($⅖) -> true;
> > is_fraction($⅗) -> true;
> > is_fraction($⅘) -> true;
> > is_fraction($⅙) -> true;
> > is_fraction($⅚) -> true;
> > is_fraction($⅐) -> true;
> > is_fraction($⅛) -> true;
> > is_fraction($⅜) -> true;
> > is_fraction($⅝) -> true;
> > is_fraction($⅞) -> true;
> > is_fraction($⅑) -> true;
> > is_fraction($⅒) -> true;
> > is_fraction(_) -> false.
> >
> > The awful actual "with a guard" version:
> >
> > is_fraction(X) when X =:= $½; X =:= $⅓; X =:= $⅔; X =:= $¼; X =:= $¾;
> > X =:= $⅕; X =:= $⅖; X =:= $⅗; X =:= $⅘; X =:= $⅙; X =:= $⅚; X =:= $⅐;
> > X =:= $⅛; X =:= $⅜; X =:= $⅝; X =:= $⅞; X =:= $⅑; X =:= $⅒ -> true;
> > is_fraction(_) -> false.
> >
> > The pretty, easy and obviously needed "with in list syntactic sugar"
> version :
> >
> > is_fraction(X) when X in "½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒" -> true;
> > is_fraction(_) -> false.
> >
> > It clearly speaks for itself.
> >
> > Cheers.
> >
> > On Mon, 25 Mar 2019 at 18:12, Florent Gallaire <fgallaire@REDACTED>
> wrote:
> >
> >
> > Frank thanks for your answer.
> >
> > You’re probably new to Erlang.
> >
> >
> > Yes, but...
> >
> > You can achieve the same with parse_transform:
> > https://github.com/mad-cocktail/gin/blob/master/README.rst
> >
> >
> > ...I can say parse_transform is not the solution Erlang needs.
> >
> > There’s no point to add new syntax to the language.
> >
> >
> > Yes we need it, an easy to use built-in "in (tuple or list I'm not
> > sure of the right semantic)" syntactic sugar for guards.
> >
> > Hope some other advices.
> >
> > Florent
> >
> > /Frank
> >
> > Hello everybody,
> >
> > I'm not very experimented in Erlang but I read carefully books and
> > official documention.
> >
> > It seems to me that the guards syntax is not as good as it should be,
> > i.e. too much verbose for multiple values.
> >
> > do(val1) -> val1;
> > do(val2) -> val2;
> > do(val3) -> val3;
> > do(val4) -> val4;
> > do(val5) -> val5.
> >
> > do(Val) when Val =:= val1; Val =:= val2; Val =:= val3; Val =:= val4;
> > Val =:= val5 -> Val.
> >
> > It's boring and error prone to write.
> >
> > Has a "in tuple" syntax already be considered ? Something like :
> >
> > do(Val) when Val in {val1, val2, val3, val4, val5} -> Val.
> >
> > Cheers
> >
> > Florent
> >
> > --
> > FLOSS Engineer & Lawyer
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
> >
> >
> > --
> > FLOSS Engineer & Lawyer
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
> >
> >
> > --
> > FLOSS Engineer & Lawyer
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
>
>
> --
> FLOSS Engineer & Lawyer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190328/383cbf5b/attachment.htm>
More information about the erlang-questions
mailing list