[erlang-questions] how: to construct and evaluate qlc expressions dynamically

Rick Pettit <>
Tue Dec 18 00:28:03 CET 2007


On Mon, Dec 17, 2007 at 04:21:17PM -0600, Rick Pettit wrote:
> I am trying to write a function which takes as input a table name and a 
> list of query constraints in the form of triplets (column id, operator, value)
> which returns a fun wrapping the equivalent qlc expression.
> 
> For example:
> 
>   QLCFun = generate_qlc_fun("favorites",[{"flavor","==","chocolate"},
>                                          {"number","<=","3"}]).
> 
> QLCFun should be roughly equivalent to:
> 
>   qlc:q([X || X <- mnesia:table(favorites),
>               X#favorites.flavor == "chocolate",
>               X#favorites.number <= 3]).
>     
> So far I've stumbled across the erl_syntax manpage and functions:
> 
>   list_comp_template/1
>   list_comp_body/1
>   list_comp/2
> 
> Am I in the ballpark, or is there a better way to do this?
> 
> Anyone know of a place where a beginner (to dynamic generation and evaluation
> of erlang code) can find decent sample code?

I meant to add that "plan B" is to construct an appropriate mnesia:select/4,
but I'm interested in dynamic generation/evaluation of code either way.

-Rick



More information about the erlang-questions mailing list