[erlang-questions] Guards syntax for multiple values

Dan Sommers 2QdxY4RzWzUUiLuE@REDACTED
Mon Mar 25 16:50:46 CET 2019


On 3/25/19 9:00 AM, Florent Gallaire wrote:

 > Yes excuse me for my wrong sentence. I was talking about
 > lists:member/2 in a guard:
 > is_fraction(X) when lists:member(X, "½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒") -> true.
 > which is not possible.

http://erlang.org/doc/reference_manual/expressions.html#guard-sequences
explains

     The set of valid guard expressions (sometimes called guard tests) is
     a subset of the set of valid Erlang expressions. The reason for
     restricting the set of valid expressions is that evaluation of a
     guard expression must be guaranteed to be free of side
     effects. Valid guard expressions are the following:

and later on, there's a list of "blessed" functions that are allowed in
guard expressions.

My experience with Erlang is entirely as a hobbyist, but what are the
pros, cons, and possibilities of expanding the list of such "blessed"
functions?  Many of the functions in the list module have no side
effects, and could make some Erlang code more concise.

That said, I've also discovered (or more likely rediscovered) that
breaking down such functions and guard clauses one more layer also leads
to more concise code.  I don't know Florent's use case, but I could
imagine breaking it down one more level into something like this:

process(X) ->
     Classification = classify(X),
     process(X, Classification).

process(X, fraction) ->
     %% process fractions here
     ok;
process(X, url_character) ->
     %% process URL characters here
     ok;
process(X, whitespace) ->
     %% process whitespace here
     ok;
process(X, UnknownClassification) ->
     %% handle an unknown X here
     ok.

Or maybe all that does is push the problem into the implementation
of classify/1.



More information about the erlang-questions mailing list