[erlang-questions] how to create user-defined guards

Bernard Duggan <>
Mon Apr 27 10:57:58 CEST 2009


Hi Ivan,
    As Bengt said, you can't make your own guards.  Why not use qlc?  
(I'm going to assume here that you're pulling out the entire record, 
since it will save me some typing :-) ):

get_students() ->
    F = fun() ->
       qlc:e(qlc:q(
          [A || A <- mnesia:table(person_table),
                   A#person_table.student =:= "Estudiante" ]))
    end,
    mnesia:transaction(F).

Apologies for any typos - doing this from memory.

Cheers,

Bernard

Ivan Carmenates Garcia wrote:
>
> Hi all,
>
> I have a problem if for example I have an mnesia cosult like this
>
> get_students()->
>         F = fun()->
>                 mnesia:select(person_table, [{#person_table{
>                         id          ='$1',
>                         name     ='$2',
>                         group    ='$3',
>                         type      ='$4',
>                         id_card ='$5',
>                         user      ='$6'},
>                         [{'=:=', {is_student,'$4'}, true}],
>                         ['$_']}])
>         end,
>         mnesia:transaction(F).
>
> is_student(X)->
>         if X == "Estudiante"->
>                 true;
>         true->
>                 false
>         end.
>
> that's only accept guards, for exaple changing  [{'=:=', 
> {is_student,'$4'}, true}], by  [{'=:=', {is_list,'$4'}, true}], then 
> that work fine
>
> because is_list is a guard, but I need to check for more complex 
> criterias, but using my own funtion criteria I just got
>
> {aborted,{badarg,[person_table,
>                   [{{person_table,'$1','$2','$3','$4','$5','$6'},
>                     [{'=:=',{is_student,'$4'},true}],
>                     ['$5']}]]}}
>
> because is_student is not valid guard.
>
> How can I simulate or make my own guard or do something else to 
> resolve this problem.
>




More information about the erlang-questions mailing list