Cover by Francesco Cesarini, Simon Thompson

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Match Specifications: The Nuts and Bolts

When covering the help function dbg:fun2ms/1, we went into a high-level explanation of its results, namely match specifications. The specifications are tuples with three elements of the format [Head, Conditions, Body]:

  • The head is used to bind and match variables and terms.

  • In the conditions, logical tests are applied on the variables. You can define your own logical tests or use predefined guards.

  • In the body part of the specification, we list a possibly empty set of predefined actions that have to be taken if the match in the head is successful and the logical conditions are met.

The objective of this section is to ensure that you understand the match specifications that result from the dbg:fun2ms/1 and ets:fun2ms/1 calls, and possibly implement some simpler ones for yourself.

The Head

The head is a list of variables, literals, and composite data types. All variables in the head are of the form '$int()', where int() is replaced by an integer of the format '0' or '1', ranging from 0 to 100,000,000. The atom '_' denotes the “don’t care” variable, and can be used if you are not interested in matching parts of a particular argument. If you instead want to match on all values and all arities without any variable bindings, you just use '_', giving a specification of the form ['_', Conditions, Body]. You might be tempted to write [[], Conditions, Body] instead, but beware, as this will only match functions of arity 0.

In the following example, we create ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required