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

Matthew Reilly matthew.reilly@REDACTED
Fri Jun 13 19:38:36 CEST 2008


Jachym Holecek wrote:
> On Fri, 13 Jun 2008 08:25:29 +0200, Doug Edmunds <dougedmunds@REDACTED>  
> 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.
>
>   
As similar approach that works for functions is to have two functions. 
The first calculates the non-guard conditions, then passes them to the 
second function which can pattern match.

e.g.:
delfix({Ky, Vy, Cy, {Kx, Vx, bb, A, B}, {Kz, Vz, b, C, D}}) ->
     delfix1({Ky, Vy, Cy, {Kx, Vx, b, A, B}, {Kz, Vz, r, C, 
D}},isBlack(A),isBlack(B),isBlack(C),isBlack(D));

delfix1({Ky, Vy, Cy, {Kx, Vx, bb, A, B}, {Kz, Vz, b, C, 
D}},_,_,true,true)  ->
     blackToken({Ky, Vy, Cy, {Kx, Vx, b, A, B}, {Kz, Vz, r, C, D}});
delfix1(...) ->
...




>      -- Jachym
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>   




More information about the erlang-questions mailing list