【问题标题】:Is there a way that we can reference the whole variable while in pattern matching of Elixir?有没有办法在 Elixir 的模式匹配中引用整个变量?
【发布时间】:2014-01-05 04:14:15
【问题描述】:

Haskell 中有一个 as-pattern,它允许我们在模式匹配时引用整个变量:

foo wholeList@(head:tail) = wholeList ++ head

变量wholeList代表原始变量。

假设head["Hello"]tail["World"],那么wholeList["Hello", "World"]

使用 as-pattern,我们可以通过串联headtail 来避免再次构造变量。

Elixir 中是否存在这样的功能?

【问题讨论】:

    标签: pattern-matching elixir


    【解决方案1】:

    是的,这是可能的。只需在您的模式中使用=

    def foo(list = [h|t]), do: list ++ h
    

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2013-05-07
      • 1970-01-01
      相关资源
      最近更新 更多