Description (from Rosetta Code)
Given a molecule's chemical formula, calculate the molar mass.
- 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
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.
This expression is shown as every subexpression set one next the other, so the
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