Is there a way we can refer to the whole variable when matching Elixir patterns?

Haskell has an as-pattern , which allows us to refer to the entire variable when matching with the pattern:

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

The variable wholeListrepresents the original variable.

Assuming that headthere is ["Hello"], and tail- ["World"], then wholeListthere is ["Hello", "World"].

Using as-pattern, we can avoid constructing the variable again by combining headand tail.

Is there such a feature in Elixir?

+4
source share
1 answer

Yes it is possible. Just use =in your template:

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

All Articles