# Chemical Calculator

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page is the answer to the task Chemical Calculator in the Rosetta Code.

### 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

### Answer

#### 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: