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

Bengt Kleberg bengt.kleberg@REDACTED
Mon Apr 27 09:15:51 CEST 2009


Greetings,

There is no way to create your own guards.

Would this be acceptable instead:

get_students()->
        F = fun(Person, Acc)->
		case Person of
		{#person_table{type      ="Estudiante"} -> {Person|Acc];
		_Else -> Acc
		end
        end,
        mnesia:foldl(F, [], person_table).

That will give you the whole record.

I have forgotten what '$_' in the match spec means (either the whole
record or just all the $n things) so here is the alternative:

get_students()->
        F = fun(Person, Acc)->
		case Person of
		{#person_table{type      ="Estudiante",
			id          =ID,
                        name     =Name,
                        group    =Group,
                        id_card =ID_card,
                        user      =User}} -> {{ID, Name, Group,
"Estudiante", ID_card, User}|Acc];
		_Else -> Acc
		end
        end,
        mnesia:foldl(F, [], person_table).


bengt

On Mon, 2009-04-27 at 01:12 +0200, 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