[erlang-questions] Pattern-matching function?
Richard Carlsson
richardc@REDACTED
Thu Oct 30 17:05:15 CET 2008
Robert Virding wrote:
> 2008/10/30 Richard Carlsson <richardc@REDACTED <mailto:richardc@REDACTED>>
>
> make_match_fun(Pstr) -> element(2, erl_eval:expr(hd(element(2,
> erl_parse:parse_exprs(element(2,
> erl_scan:string(lists:flatten(io_lib:format("fun (~s=__P) ->
> {matched,__P}; (_) -> fail end.", [Pstr]))))))),
> erl_eval:new_bindings())).
>
> This will, via erl_eval, produce a fun that can be used like you wanted:
>
> F = make_match_fun("{X,Y}")
>
> F({1,2}) ==> {matched, {1,2}}
>
> F({}) ==> fail
>
>
> My macro basically does this is easier, at least I think so.
Not quite. With your suggestion, he could still only use a fixed number
of patterns, known at compile time. But it sounded like he wanted to
generate patterns somehow on the fly, in which case he'd need to do
either dynamic compilation or interpretation.
/Richard
More information about the erlang-questions
mailing list