Erlang中的一流模式?(备择方案)

Str*_*s3D 4 erlang elixir pattern-matching first-class

有没有办法在 Erlang 中创建一流的模式?我需要能够创建模式并将其作为参数传递给其他函数,但我知道模式在 Erlang 中并不是一流的。我还研究了 Elixir,但就模式而言,它似乎没有提供更多信息。

我想知道是否有人想出了一个简单的解决方案来解决这个问题。我正在考虑尝试实现这样的东西:

% Instead of using variables, we would just use uppercase atoms which would serve as vars
% A passable pattern
Pattern = {ok, 'Result'}. 

% Custom function to check for matches
match(pattern, {ok, [1,2,3]}). % => true
Run Code Online (Sandbox Code Playgroud)

我是 Erlang 的新手,所以也许这完全没有必要。也许有一个图书馆可以做这种事情?

任何意见是极大的赞赏。提前致谢!

Pas*_*cal 5

我不知道是否已经存在可以满足您要求的功能,但是您可以像这样轻松实现它:

-module (match).

-compile([export_all]).

-define(MF(S), fun(S) -> true; (_)->false end).


match(F,V) -> F(V).


test() ->
    Pattern = ?MF({ok,_}),
    false = match(Pattern,{error,reason}),
    true = match(Pattern,{ok,[1,2,3]}).
Run Code Online (Sandbox Code Playgroud)