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

Fuad Tabba <>
Fri Jun 13 11:43:54 CEST 2008

Thanks Doug and Jachym.

Jachym is right, Doug's suggestion would work if the guard was inside my
function rather than at a top level context. I know that I could rewrite my
code and convert it to a bunch of if statements, but I can't think of a way
to write such code without it looking ugly.

Being able to use a function with a guard seems like it would be the most
elegant solution.

Any suggestions?


On Fri, Jun 13, 2008 at 8:35 PM, Jachym Holecek <>

> On Fri, 13 Jun 2008 08:25:29 +0200, Doug Edmunds <>
> wrote:
>> check out the discussion on
>> http://wrongnotes.blogspot.com/2007/09/little-erlang.html
>> "The solution is to call the function and store it in a
>> variable before we use it in the guard conditions."
> Yes, but it's a solution to the particular case you hit
> working with if/case/fun; not to the general inability
> to use custom functions in guards.
> Clearly, you can't "store it in a variable" when you're
> at toplevel context (which is what Fuad has been asking
> about) or when the object you'd like to inspect comes
> from the pattern immediately preceding the guard.
>    -- Jachym
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080613/4441610d/attachment.html>

More information about the erlang-questions mailing list