6 min read, 1365 words
Mathematical Inquiries
As we have previously witnessed, one may specify which type of data a particular constant or variable is able to store, such as the string type β used to store an aggregation of characters and even numbers β which thence is treated as mere text. Using this type of data is beneficial for text which is to be printed onto the screen or for storing names and addresses, yet it is ineffective for mathematical tasks, such as the subtraction or addition of two numbers. Let us inspect the following example: β
# string-addition.b
# Include statements and declaration have been left out, same as previous code snippets
init (ctxt: ref Draw->Context, args: list of string) {
sys = load Sys Sys->PATH;
num1: con "1";
num2: con "2";
sys->print(num1 + num2);
}
Herein, two constants, namely num1 and num2, have been defined, each containing a number placed inside quotation marks; hence, both of these are, in actuality, strings. This, in turn, implies that Limbo does not treat them as numbers per se; nay, instead, they get treated as text, which is why the print statement outputs 12 instead of 3.
Thus, it is to be remarked, the + operator situated within the print statement does not perform a mathematical function in this particular instance, as its function is altered when applied to strings; for there, the operator functions as a concatenator of sorts, simply joining two pieces of text with one another. If we wished to add numbers and receive an accurate mathematical result, we must use integers β or, indeed, other type of numbers β instead; and, if we desired, we could simply create a new function titled add, whose sole purpose it is to accept two integers as its arguments, add them together and return the result. Such a program could be written as follows: β
# plus-function.b
implement IntAddition;
include "sys.m";
include "draw.m";
sys: Sys;
IntAddition: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
};
add: fn(num1: int, num2: int): int;
add(num1: int, num2: int): int {
return num1 + num2;
}
init (ctxt: ref Draw->Context, args: list of string) {
sys = load Sys Sys->PATH;
sys->print("%d", add(2, 4));
}
Line 12 of this piece of code contains the declaration of the add function, taking two integers as its arguments and returning but one integer, namely the sum of the addition. In Limbo, it is imperative to declare the type of data a function may return; this is wherefore the : int; part is to be found at the end of the function declaration. The previously studied greet function, too, contained such a statement; therein, however, it is supposed to return a string instead of an integer, for the name of a person cannot be stored within an integer.
An integer, should you be unfamiliar with this terminology, is a whole number, id est a number which one can depict without the usage of a fractional component; an example of a number with a fractional component would be 2.2. Therefore, should you supply the newly created add function with a number containing fractions, you shall be greeted by the following error: 'add': argument type mismatch: expected int saw '2.200000047683716' of type real. Here I provided not an integer, but a number containing a fraction, namely 2.2.
If you instead yearned to use numbers containing fractional components, you would be required to use real numbers, for these types of numbers contain both integers and fractions. We shall consider the changes to be made to our current code so that it may accept real numbers shortly; first, let us continue by examining the current code.
Lines 13 through 15 see the implementation of our function, which shall simply return the sum of both integers it was supplied with.
Lastly, line 19 contains the print statement we have used numerous times before; yet herein, its structure appears to deviate slightly from that which was used priorly, for here it contains two statements instead of the single used previously. As I hope you recall, print may take only a string as its argument and thus, supplying it with an integer directly will yield an error. For example: β
sys->print(add(2, 4));
This would return the following error upon compilation: 'sys->
print': argument type mismatch: expected st-ring saw 'add(2, 4)' of type real. Therefore, if we wish for print to actually print the sum of our addition, we must do thusly: β
sys->print("%d", add(2, 4));
Here, the first argument supplied to print is "%d" which, I assume, stands for digit; it is used as a reference to the second argument, namely the execution of our add function. Please also note that, henceforth, I shall be calling the "%d" strings value reference code. The same was required in our aforementioned greet program, wherein it was written as follows: β
sys->print("%s", greet("Hex"));
Herein, the "%d" has been replaced by an "%s" instead, for the greet function does not return an integer, but a string instead.
Thus, the first argument receives the value of the second argument and, as it is located within a string, print is able to use it. You may append as many argument pairs as you wish, so long as you correctly specify their data type using the % parameters. Hence, one may combine both the greet and add functions within the print statement as follows: β
sys->print("%d%s", add(2, 4), greet("Hex"));
This would return both the sum of our addition and a text which reads "Hello Hex".
If, instead, we longed to create a function which allowed us to add real numbers, we would be required to change the code accordingly: β
# plus-function-real.b
implement RealAddition;
include "sys.m";
include "draw.m";
sys: Sys;
RealAddition: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
};
add: fn(num1: real, num2: real): real;
add(num1: real, num2: real): real {
return num1 + num2;
}
init (ctxt: ref Draw->Context, args: list of string) {
sys = load Sys Sys->PATH;
sys->print("%f", add(2.2, 4.0));
}
Herein, we altered the data types our function is able to receive from int to real, changed the data type it will return to real and changed the formatting within the print statement to the value reference code "%f" instead; this, I believe, stands for floating point number. Furthermore, we are required to convert all integers β in this example, 4 β to a real number by adding a .0 behind it. The reason behind this is that henceforth, our function may only accept real numbers; and even though integers are technically part of the real numbers, we must nonetheless supply our function with a number containing a fraction β even if said fraction is zero.
Withal, it would be possible to convert an integer into a real number by simply adding real in front of it; we could thus revise the print statement thusly: β
sys->print("%f", add(2.2, real 4));
In addition to the aforementioned data types, there exists another one titled big, which is, essentially, a regular integer which can hold larger numbers; its value reference code is %bd which can be remembered as meaning big digit. There are, however, even further data types to be found within this language, but we shall be discussing them only when we are required to use them.
Let us now examine a simple way of handling user input via command line arguments, wherewith we may improve our previously created greet program.