Elixir Pattern Matching

Source: Internet
Author: User

Elixir pattern matching just a little bit unaccustomed, in Elixir which = operators are called match operators

IEX > x = 1
1
IEX (+) > x
1
IEX (+) > 1 = x
1
IEX (+) > 2 = x
* * (Matcherror) No match of right hand side value:1

1=x legally equals 1.

An error occurs when the 2=x sides are unequal MatchError

Match list

IEX (2) > A = [1]
[1]
IEX (3) > [h|t] = a
[1]
IEX (4) > H
1
IEX (5) > t
[]

with [h|t] = A---"[h|t]=[1]--" H matches to 1 T is empty

if [h|t] = []

* * (Matcherror) No match of right hand side value: []

Matching tuples

IEX (+) > {A, B, c} = {: Hello, ' word ', 33}
{: Hello, "word", 33}
IEX (> a)
: Hello
IEX (Panax Notoginseng) > B
"Word"
IEX (> C)
33

Both sides do not match

IEX ($) > {A, B, c} = {: Hello, ' word '}
* * (Matcherror) No match of right hand side value: {: Hello, "word"}

The first one on the right is not: OK also does not match

IEX (+) > {: OK, result} = {: OK, 13}
{: OK, 13}
IEX ($) > result
13
IEX > {: OK, result} = {: Error,: oops}
* * (Matcherror) No match of right hand side value: {: Error,: oops}

Elixir Pattern Matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.