函数定义的形式参数中的模式匹配

发布于 2024-08-09 19:43:47 字数 463 浏览 5 评论 0原文

这是我在 erlang 代码中见过几次的东西,但是谷歌很难找到这个例子(下面链接中的第一个代码块):

http://www.process-one.net/en/wiki/ejabberd_HTTP_request_handlers/

在process的函数定义的“head”中/2

process(_LocalPath = ["world"], _Request) ->

第一个参数/参数有模式匹配;

这是否类似于守卫,因此仅当传递给 process/2 的第一个参数是字符串“world”,或者“world”是某种默认参数时,才会执行以下子句?或者我完全误解/猜错了?

Here's something I've seen in erlang code a few times, but it's a tough thing to google and I can only find this example (the first code block in link below):

http://www.process-one.net/en/wiki/ejabberd_HTTP_request_handlers/

In the "head" of function definition of process/2

process(_LocalPath = ["world"], _Request) ->

there is a pattern match on first parameter / argument;

Does this act similarly like a guard, so the following clause will be executed only if the first argument passed to process/2 is string "world", or is "world" some kind of a default argument? Or i completely misunderstood/ mis-guessed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-08-16 19:43:47

是的,这是一个模式匹配。如果第一个参数是具有单个元素的列表(该元素是字符串“world”),则将执行该子句。

Yes, this is a pattern match. The clause will be executed if the first argument is a list with a single element, the element being the string "world".

演出会有结束 2024-08-16 19:43:47

你是对的: _LocalPath = ["world"] 充当模式“守卫”。如果函数“process”的第一个参数不等于[“world”],则模拟器将继续查找匹配项。

需要注意的一件事: _LocalPath 充当“装饰器”以增强可读性,因为标识符以下划线开头。

You are correct: _LocalPath = ["world"] acts as a pattern "guard". If the first parameter to the function "process" isn't equal to ["world"], then the emulator proceeds to find a match down.

One thing to note: _LocalPath serves as "decorator" to enhance readability since the identifier starts with an underscore.

黑凤梨 2024-08-16 19:43:47

模式中的 = 用于别名,它基本上允许你鱼和熊掌兼得。它既进行正常的模式匹配,又将变量绑定到整个匹配的数据。如果您需要整个数据,那么它很实用,因为它使您无需重建数据。您可以在模式中的任何位置使用它。与守卫无关。

_LocalPath 中的 _ 开头的变量也告诉编译器如果不使用该变量,不要抱怨。通常,如果您绑定变量但不使用它们,编译器会发出一些抱怨声。除此之外,名称以 _ 开头的变量没有什么特别之处,您可以像使用任何变量一样使用它们。

唯一真正特殊的变量是_,即匿名变量。它始终匹配并且从不绑定,因此您可以将其用作匿名占位符。这就是它首先存在的原因。

我个人很少使用以 _ 开头的变量,而更喜欢仅使用 _。我还觉得用不必要的东西弄乱模式是一件坏事,所以我不会在这样的文档中使用别名。我会写:

%% process(LocalPath, Request) -> ... .

process(["world"], _) ->

或者如果您愿意的话也可以是类型声明。我认为使代码更短、更清晰。

The = in a pattern is used for an alias, it basically allows you to have your cake and eat it. It both does a normal pattern match and binds a variable to the whole matched data. It is practical if you need the whole data as it saves you having to reconstruct it. You can use it anywhere in a pattern. It has nothing to do with guards.

Starting a variable with a _ as in _LocalPath is too tell the compiler not to complain if this variable is not used. Normally the compiler whines a bit if you bind variables and don't use them. Apart from this there is nothing special about variables whose names start with _, you can use them as you would any variable.

The only really special variable is _, the anonymous variable. It always matches and is never bound so you can use it as an anonymous place holder. Which is why it exists in the first place.

I personally very rarely use variables starting with _ and prefer to use just _. I also feel that cluttering up patterns with unnecessary things is a Bad Thing so I wouldn't use aliases for documentation like that. I would write:

%% process(LocalPath, Request) -> ... .

process(["world"], _) ->

or perhaps a type declaration if you prefer. Keeps the code shorter and more legible, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文