[erlang-questions] How to create a function using merl given a list of clauses

Frans Schneider fchschneider@REDACTED
Wed Feb 14 13:31:07 CET 2018


Dear list,

 From a set of user defined rules, I want to create functions of the form:

fun([V1, V2]) ->
     fun () when V1 < 1, is_atom(V2) -> foo();
         () when V2 =/= b -> bar();
         () -> false
     end
end.

The number of arguments, clauses and guards differ. Guards always are 
ANDed or andalso should be used.

To build the internal fun I do something like:

...
-include_lib("syntax_tools/include/merl.hrl").
...
tst() ->
     %% This is the end result I want
     V1 = 10,
     V2 = a,
     Func = ?Q(["fun () when _@REDACTED@ < 1, is_atom(_@REDACTED@) -> foo();",
            "    () when _@REDACTED@ =/= b -> bar();",
            "    () -> false ",
            "end"]),
     %% io:format("Func ~p~n", [Func]),
     merl:print(Func),

     %% This is the data produced by some function
     Guard1 = ?Q("_@REDACTED@ < 1"),
     Guard2 = ?Q("is_atom(_@REDACTED@)"),
     Guard3 = ?Q("_@REDACTED@ =/= b"),
     GSeq1 = [Guard1, Guard2],
     GSeq2 = [Guard3],

     %% Now start assembling the fun
     Clause1 = ?Q("() when _@REDACTED -> foo()"),
     Clause2 = ?Q("() when _@REDACTED -> bar()"),
     Clauses = [Clause1, Clause2],
     merl:print(Clauses),

     %% And this gives the error: syntax error before: 'fun'"
     Func1 = ?Q("fun _@REDACTED end"),
     merl:print(Func1).

I just can't figure out a way to make the last step assembling the fun work.

Your suggestions are highly appreciated!

Frans




More information about the erlang-questions mailing list