[erlang-questions] (newbie) Using Functions as Guards in Erlang

Thomas Lindgren <>
Sat Jun 14 12:56:46 CEST 2008

--- Paul Mineiro <> wrote:

> Couldn't one implement the "evaluate into in a
> variable, then match"
> strategy with a parse transform?

I realize I might have been a bit unclear in the
example. The Ei in a pseudo-guard 
   catch (E1 andalso E2... andalso En) 
would be metavariables standing for expressions. So a

fun({byte, X},Y) 
  when X >= 0, X =< 16#ff, integer(Y), X < Y -> ...;

would become something like

fun(T1, T2) ->
  case catch 
   (begin {byte, X} = T1, Y = T2, true end 
   andalso X >= 0 andalso X =< 16#ff
   andalso integer(Y) andalso X < Y) of
  true ->
  _ ->
    <next clause>

To answer your question: In principle, yes, but you'd
have to be careful to ensure that the transformation
was efficient. First, there is possible code
duplication. Second, the transform has to play nicely
with the compiler. The example above would probably
confuse the pattern match compiler, for instance,
which might slow things down considerably.



More information about the erlang-questions mailing list