Patterns look the same as terms – they can be simple literals like
atoms and numbers, compound like tuples and lists, or a mixture of both.
They can also contain variables, which are alphanumeric strings that
begin with a capital letter or underscore. A special "anonymous
variable", _ (the underscore) is used when you don't care about the
value to be matched, and won't be using it.
A pattern matches if it has the same "shape" as the term being matched, and atoms encountered are the same. For example, the following matches succeed −
The following examples of pattern matching will fail.
A pattern matches if it has the same "shape" as the term being matched, and atoms encountered are the same. For example, the following matches succeed −
- B = 1.
- 2 = 2.
- {ok, C} = {ok, 40}.
- [H|T] = [1, 2, 3,4].
The following examples of pattern matching will fail.
- 1 = 2.
- {ok, A} = {failure, "Don't know the question"}.
- [H|T] = [].
No comments:
Post a Comment