Chemical Calculator

Description (from Rosetta Code)

 Given a molecule's chemical formula, calculate the molar mass. Introduction A molecule consists of atoms. E.g. water, H2O, has two hydrogen atoms and one oxygen atom The mass of H2O is 1.008 * 2 + 15.999 = 18.015 An atom name consists of one upper-case letter followed by zero, one or two lower-case letters. H (Hydrogen) He (Helium) Uue (Ununennium) The number of atoms is stated behind the atom or atom group An atom group is specified using parenthesis. E.g. Butyric acid, (CH3)2CHCOOH, has two CH3 groups A group may contain other groups, e.g. COOH(C(CH3)2)3CH3 Link

Preamble

There is a Fōrmulæ package for chemistry being developed at the moment of writing this article, but it has enough features to accomplish this task.

Chemical elements

There is a Fōrmulæ expression for each chemical element, as follows:

Atomic number Element expression
1 Chemistry.Element.Hydrogen
2 Chemistry.Element.Helium
3 Chemistry.Element.Lithium
:  :

They are shown as their chemical symbol, it is H for Hydrogen, He for Helium, etc.

Chemical compounds

There are two Fōrmulæ expression to make chemical formulae:

The Chemistry.Compund expression represents a composition between two or more chemical elements, but it can also contain either Chemistry.Compond or Chemistry.Group (see below) expressions. For example, the composition of the chemical elements C and O results in the compund CO.

The Chemistry.Group represents an expression that is present several times. It contains exactly two subexpressions, the first one is the chemical element, Chemistry.Compound or Chemistry.Group expression, and the second one is the number of times. For example, grouping the O chemical element two times results in O2

The following are examples of chemical formulae created with the previous expressions:

Let us see the structures of the given formulae, by the use of the `ToTree` expression:

Please note that there is no expression for parentheses, they are not necessary because there is no ambiguity in the structure. However, visualizers of Chemistry.Compund and Chemistry.Group expressions show them when it is necessary, in order to look natural to humans.

Information of chemical elements

There are the following expressions, intended to retrieve information of chemical elements, such as the atomic mass, which is necessary for this task.

Expression Explanation
Chemistry.FromNumber Retrieves the chemical element expression given it atomic number
Chemistry.FromSymbol Retrieves the chemical element expression given its symbol
Chemistry.GetNumber Retrieves the atomic number of a chemical element expression
Chemistry.GetSymbol Retrieves the symbol of a chemical element expression
Chemistry.GetName Retrieves the name of a chemical element expression
Chemistry.GetMass Retrieves the atomic mass of a chemical element expression

The following produces a matrix containing, for the first 20 chemical elements, the element expression, its atomic number, its symbol, its name and its atomic mass.

Please note that although the first and third columns seem to be the same, the first one is the Fōrmulæ expression of the chemical element, while the third one is a Text.String expression.

Solution

We are now ready to write a function to calculate the molar mass:

Notes
• The `Tag(expression)` expression retrieves the tag of an expression. For example, when it is called on an Chemistry.Compund expression, it retrieves the Text.String expression representing the string "Chemistry.Compound".
• The `#Expression` retrieves the cardinality of the expression, this is, the number of subexpressions it has. If the expression is a Chemistry.Compund it gives the number of elements being composed.
• If the expression given as parameter is a Chemistry.Compound expression, the molar mass is the sum of the molar masses of each component. Note that this function is recursivelly called.
• If the expression given as parameter is a Chemistry.Group expression, the molar mass is the product of the number of the group (the second component) and the molar mass of the expression (the first component). Note that this function is recursivelly called.
• Elsewhere, the result is the call of the Chemistry.GetMass with the expression given as parameter.

Use cases

Let us calculate the molar mass of the chemical formulae given as use cases:

Using it symbolically

Fōrmulæ is a symbolic language. Although chemical elements expressions are intended to be used to create chemical formulae, other expressions can be used, specially symbols, as in the following examples:

Example 1. Using a symbol to denote and unspecified number of repetitions in a Chemistry.Group expression

For this exercise, n is a free symbol (a symbol with no associated value).

Example 2. Using a symbol to denote an unspecified chemical element

For this exercise, X is a free symbol (a symbol with no associated value).

Example 3. Using symbols to denote an unspecified chemical element and an unspecified number of repetitions

For this exercise, X and n are free symbols (symbols with no associated values).

Example 4. Using symbols to denote different unspecified chemical elements

For this exercise, X, Y and Z are free symbols (symbols with no associated values).

Example 5. Other combinations

For this exercise, X, Y, Z, n and m are free symbols (symbols with no associated values).