Skip to content

Examples

This section demonstrates the capabilities to draw diagrams. It is not a tutorial on how to use the project, but rather a showcase of what the project can do.

Variable assignment

Variable assignment is a fundamental concept that involves giving a name to a piece of data. It allows for storage and manipulation of information within a computer program.

When a value is assigned to a variable, it is akin to instructing the system, "Remember this information by this name." It's like storing a number, a word, or any other data type under a chosen label. The label can then be utilized in the code to perform operations, make decisions, or simply reference that piece of data.

Textual description:

Pseudocode style using ":=" symbol:

Pseudocode style using "<-" symbol:

Type annotation

Type annotation is the practice of explicitly specifying the data type of a variable. This helps in defining the kind of information that a variable can hold, promoting code clarity and preventing potential errors.

When annotating the type of a variable, additional information is provided to both programmers and the compiler or interpreter. It is similar to informing the system that a variable is meant to store numbers, text, or some other specific data type.

Textual description:

C programming language style:

Python style:

Loop

Textual Description (Test-first loop):

Pseudocode style (Test-first loop):

Python style:

C style:

Functions

Python style:

C style:

Bash style:

Sequence calculation

Sequence calculation is a pattern of algorithm which reduces a sequence of elements to one element, which is archieved by repeated applying functions or operations to two elements and combining them into one.

Sequence calculation on integers using addition:

Counting

Counting is a pattern of algorithm which reduces a sequence of elements to a natural number. It looks at every element in a sequence and see how many of those elements have a certain property.

Counting the number of primes numbers in an interval:

Maximum selection and minimum selection

Maximum selection and minmium selection are a similar pattern algorithm. Both of them reduce a sequence of elements to one element. This element is either the "maximum" or the "minimum" element of the sequence.

Maximum selection on integers:

Minimum selection on integers:

Search is a pattern of algorithm which reduces a sequence of elements to one element. The element is chosen based on whether it has a certain property or not. However, such an element might not exist in the sequence.

Searching for a prime number in a sequence of integers, defaults to "-1" when the sequence does not contain a prime number:

Selection

Selection is a pattern of an algorithm which reduces a sequence of elements to one element. Unlike Search, at least one such element exists.

Selecting the last prime number in a sequence of integers:

Decision

Decision is a pattern of an algorithm which reduces a sequence of elements to a boolean value. If an element in a sequence satisfies a certain property, it resolves to true, and false otherwise.

Decide whether a sequence of integers contains a prime number or not:

Algorithmic patterns over intervals

An object-oriented programming summary of interval-based patterns of algorithms.

Summation

Consider an arbitrary set \(H\), a binary operation \(\text{+}:H\times H\to H\) called addition, a neutral element \(0\in H\) called zero, such that, \(0+h\mapsto h\) for all \(h\in H\), and a unary function \(f:[m, n]\to H\) which maps integers in interval \([m,n]\subseteq\mathbb{Z}\) to \(H\).

The summation pattern takes an integer interval, perform operations on it, and reduce it down a single element.

\[ \begin{aligned} A&:=(m:\mathbb{Z}, n:\mathbb{Z}, r:\mathbb{H})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left(r=\sum_{i=m}^{n} f(i)\right) \end{aligned} \]

Counting

Consider a predicate function \(\text{pred}:[m,n]\to\mathbb{L}\) which maps each integer in the interval \([m,n]\subseteq\mathbb{Z}\) to \(\mathbb{L}:=\{\top,\bot\}\).

In general, the counting pattern takes an interval and count the number of elements which satisfy a condition.

\[ \begin{aligned} A&:=(m:\mathbb{Z}, n:\mathbb{Z}, r:\mathbb{Z})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left(r=\sum_{\substack{i=m\\ \text{pred}(i)}}^{n} 1\right) \end{aligned} \]

Maximum selection

Consider a totally-ordered set \(H\), a total-ordering binary relation \(\gt\subseteq H\times H\) called greater than, and a unary function \(f:[m, n]\to H\) which maps integers in interval \([m,n]\subseteq\mathbb{Z}\) to \(H\).

The maxmima selection pattern takes a integer interval, and returns the maximal element. The semantic meaning of maximal element is different based on context and implementation.

\[ \begin{aligned} A&:=(m:\mathbb{Z}, n:\mathbb{Z}, r:\mathbb{H}, k_{r}:\mathbb{Z})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left((r,k_{r})=\max_{i=m}^{n} f(i)\right) \end{aligned} \]

Conditional maximum selection

Consider a totally-ordered set \(H\), a total-ordering binary relation \(\gt\subseteq H\times H\) called greater than, a predicate function \(\text{pred}:[m,n]\to\mathbb{L}\) which maps each integer in the interval \([m,n]\subseteq\mathbb{Z}\) to \(\mathbb{L}:=\{\top,\bot\}\), and a unary function \(f:[m, n]\to H\) which maps integers in interval \([m,n]\subseteq\mathbb{Z}\) to \(H\).

The conditional maximum selection pattern takes an integer interval, and returns the maximal element which satisfy the given condition.

\[ \begin{aligned} A&:=(m:\mathbb{Z}, n:\mathbb{Z}, u:\mathbb{L}, r:\mathbb{H}, k_{r}:\mathbb{Z})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left((u, r,k_{r})=\max_{\substack{i=m\\\text{pred}(i)}}^{n} f(i)\right) \end{aligned} \]

Selection

Consider a arbitrary set \(H\) and a predicate function \(p:[m,n]\to\mathbb{L}\) which maps each integer in the interval \([m,n]\subseteq\mathbb{Z}\) to \(\mathbb{L}:=\{\top,\bot\}\).

The selection pattern takes an integer interval and returns an element which satisfy the given condition with the assumption that such a element exists.

\[ \begin{aligned} A&:=(m:\mathbb{Z}, k:\mathbb{Z})\\ \text{pre}&:=m=m'\land (\exists k\in\mathbb{Z}: k\ge m\land p(k))\\ \text{post}&:=\text{pre}\land\left(k=\underset{i\ge m}{\text{SELECT }} p(i)\right) \end{aligned} \]

Consider a arbitrary set \(H\) and a predicate function \(p:[m,n]\to\mathbb{L}\) which maps each integer in the interval \([m,n]\subseteq\mathbb{Z}\) to \(\mathbb{L}:=\{\top,\bot\}\).

The linear search pattern takes an integer interval and returns the first element which satisfy the given condition, though such an element might not exist.

\[ \begin{aligned} A&:=(m:\mathbb{Z},n:\mathbb{Z},u:\mathbb{L}, k:\mathbb{Z})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left((u, k) = \underset{i=m}{\overset{n}{\text{SEARCH }}} p(i)\right) \end{aligned} \]

Consider a totally-ordered set \(H\), an element \(h\in H\), a total-ordering binary relation \(\gt\subseteq H\times H\) called greater than, and a monotomically-increasing unary function \(f:[m,n]\to H\) which maps integers in the interval \([m,n]\) to \(H\).

The binary search pattern takes an integer interval and decide whether it contains specific element or not.

\[ \begin{aligned} A&:=(m:\mathbb{Z},n:\mathbb{Z},h: H, u:\mathbb{L}, k:\mathbb{Z})\\ \text{pre}&:=m=m'\land n=n'\\ \text{post}&:=\text{pre}\land\left(u=\left(\exists i\in\mathbb{Z}:i\in[m,n]\land f(i)=h\right)\right)\land\left(u\implies (k\in[m,n]\land f(k)=h\right) \end{aligned} \]