- 1 Description (from Rosetta Code)
- 2 Answer
- 2.1 Preamble
- 2.2 Solution
- 2.3 Use cases
- 2.4 Using it symbolically
- 2.4.1 Example 1. Using a symbol to denote and unspecified number of repetitions in a Chemistry.Group expression
- 2.4.2 Example 2. Using a symbol to denote an unspecified chemical element
- 2.4.3 Example 3. Using symbols to denote an unspecified chemical element and an unspecified number of repetitions
- 2.4.4 Example 4. Using symbols to denote different unspecified chemical elements
- 2.4.5 Example 5. Other combinations
Description (from Rosetta Code)
| Given a molecule's chemical formula, calculate the molar mass.
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.
There is a Fōrmulæ expression for each chemical element, as follows:
|Atomic number||Element expression|
They are shown as their chemical symbol, it is H for Hydrogen, He for Helium, etc.
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
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.
|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.
We are now ready to write a function to calculate the molar mass:
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".
#Expressionretrieves 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.
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).