diff --git a/code-snippets/plus-function.b b/code-snippets/plus-function.b new file mode 100644 index 0000000..0b9cdf7 --- /dev/null +++ b/code-snippets/plus-function.b @@ -0,0 +1,20 @@ +# plus-function.b +implement StringAddition; + +include "sys.m"; +include "draw.m"; +sys: Sys; + +StringAddition: 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("%s", string add(2, 4)); +} diff --git a/code-snippets/string-addition.b b/code-snippets/string-addition.b new file mode 100644 index 0000000..8caf48c --- /dev/null +++ b/code-snippets/string-addition.b @@ -0,0 +1,11 @@ +# 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); +} diff --git a/inferno.pdf b/inferno.pdf index b946371..e458953 100644 Binary files a/inferno.pdf and b/inferno.pdf differ diff --git a/inferno.tex b/inferno.tex index 5132cd6..5c94da6 100644 --- a/inferno.tex +++ b/inferno.tex @@ -667,7 +667,19 @@ Upon having analysed the Hello World program, let us continue by learning about \addcontentsline{toc}{chapter}{Mathematical Inquiries} \epigraph{``All truths are easy to understand once they are discovered; the point is to discover them''}{\textit{Galileo Galilei}} +As we have previously witnessed, one may specify which type of data a particular constant or variable is able to store, such as the \texttt{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 suppposed to be shown on screen, yet it is ineffective when used to perform mathematical tasks, such as the substraction or addition of two numbers. Let us inspect the following example: — + +\lstinputlisting{code-snippets/string-addition.b} + +Herein, two constants, namely \texttt{num1} and \texttt{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 \texttt{print} statement outputs \textit{12} instead of \textit{3}. + +Thus, it is to be remarked, the \texttt{+} operator situated within the \texttt{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 \textit{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 \textit{integers} instead; and, if we desired, we could simply create a new functioned titled \texttt{add}, whose sole purpose it is to take two numbers which we supply, add them together and return the result. Such a program could be written as follows: — + +\lstinputlisting{code-snippets/plus-function.b} + \newpage +\thispagestyle{empty} + \mbox{} %%%%%%%%%% Appendix %%%%%%%%%% \part*{Appendix}