Introduction to the Values of a Program
Introduction to the Values of a Program
Fundamentals of Values
Introduction to Variables
A variable is a section of the computer memory used to temporarily hold a value.
Reserving a portion of memory is referred to as declaring a variable. There are rules you must follow.
To declare a variable, use an operator named let. Based on this, declaring a variable in F# is also referred to as "let binding".
Follow the let keyword by a name for the variable and assign a value to it using the assignment operator "=". The formula to follow is:
let variable-name = Value
Here is an example:
let x = 16
Although we declared and initialized the variable in one line, if you want, you can put the initialization on another line. If you decide to do this, the code on the next line must be indented. Here is an example:
let x = 16
When you write an F# statement as done above, you can end it with a semicolon. The above declaration can also be written as follows:
let x = 16;
In this case, and for such a statement in F#, the semicolon is optional. Remember that if a statement continues on the next line, you must indent that next line, including its semicolon if any. Here is an example:
let x = 16;
Letting Many Declarations
Instead of declaring one variable, you may want to declare many variables. To do this, you can declare each variable on its line, using its own let keyword and its own initialization. Here are examples:
let x = 16 let y = 24 let z = 58
As an alternative, you can use one let keyword and list the variables separated by commas with the group followed by the assignment operator "=". After the assignment, give the list of values for each variable in the order they appear in the initialization. Here is an example:
let x, y, z = 16, 24, 58 // x = 16; y = 24; z = 58;
The variables don't have to be of the same type. Here is an example:
let x, y, z = 16, "John", 'M';
Remember that if the line of initialization is too long, you can continue it on the next line but remember to indent the code on the next line. Here is an example:
let x, y, z = 16, "John", 'M';
The name of a variable must follow some rules:
array | do | downto | for | in | let | to | while |
fun | function | mutable | ref | yield | |||
inline | abstract | and | as | assert | base | begin | class |
default | delegate | do (Function) | done | downcast | elif | ||
else | end (class) | end (if) | exception | extern | false | finally | |
for | if | ignore | inherit | ||||
interface | internal | lazy | match | member | module | ||
namespace | new | not | null | of | open | ||
or | override | private | public | rec | |||
static | struct | ||||||
then | true | try | type | unit | upcast | ||
use | val | void | when | with |
asr | atomic | break | checked | component | const | constraint |
constructor | continue | eager | event | external | fixed | functor |
include | land | lor | lsl | lsr | lxor | method |
mixin | mod | object | parallel | process | protected | pure |
sealed | sig | tailcall | trait | virtual | volatile |
let age = 21 let salary = 36880 let category = 5
let memberCategory = 5 let numberOfPages = 488
let _number = 38 let _dc101 = 202 let _metroDowntown10 = 20910
Introduction to User-Based Issues
Introduction to Printing a Value to the Screen
Probably the most iportant aspect of a program for a user is the value, or the values, that the program displays. To make this possible, the F# language provides a display a function named printf. The most fundamental value you can display in an application is a string. To do that, type printf followed by the value between double-quotes. Here is an example:
printf "Hello from F#"
Building the Project
For you, as a programmer, your primary role is to create a project and make it work. Of course, you also have to test it and deal with other types of problems related to your product. After starting a project, while you are working on it, you must build it to verify whether everything is alright. To assist you in building a project, the F# language comes with an internal program named a compiler (actually, it is more complex than that we will not care about that for now). To start the building process, if you are working in Microsoft Visual Studio, on the main menu, you can click Build and click Build Solution. If everything in your program is, or seems, alright, nothing bad will happen. If there is a problem in your program, an error will display.
Executing the Application
In order to see the results of your project, your users must execute your application. Before getting there, you also must execute your project to see that it looks like. When you have built your project and there is no error, the compiler creates an application, named an executable, in the final folder of your project ("Drive Letter:\Users\user-name\source\repos\project-folder\project-name\project-name\bin\Debug\net7.0"). You can access the application in that folder. Instead of going all this, Microsoft Visual Studio allows you to build you project and immediately execute it, right in Microsoft Visual Studio. To do this, on the main menu of Microsoft Visual Studio, click Debug and click Start Without Debugging.
Next Line Please
One of the characteristics of the printf function is that, when it has displayed its value, it keeps the caret at the end of that value, on the same line. This means that the next operation would execute on that same line. In some cases, you will want to move the carret to the line so that the next operation would excute on that next line. To make this possible, the F# language provides a function named printfn. It mostly works like the printf function. That is, add the desired double-quoted value after printfn. Here is an example:
printfn "Hello from F#"
Practical Learning: Starting a Project
Displaying a Value In Print
The primary job of an application is to display values, and there are many types of those values. You can start by declaring a variable and initializing it. To let you display a value using printf and/or printfn, these functions have a pre-procesor represented by the character %. It must be followed by a format, which is represented by a character or many symbols, depending on the type of value. The % and its subsequent character(s) must be included in the double-quotes of a printf and/or printfn, these function. After those quotes, add the name of the variable.
Practical Learning: Creating a string
let message = "Welcome to the wonderful world of functional programming!";
let message = "Welcome to the wonderful world of functional programming!";
printfn "%s" message;
Introduction to Data Types
Fundamentals
When you declare (or bind) a variable and assign a value to it, if you don't explicitly indicate its type, the compiler implicitly determines the variable's type. Still, F# supports different types of values.
Storing a Value in Bits
When you declare a variable, the compiler must put, or store, that value in the computer memory. To make this possible, the computer memory organizes its storage area is locations that resemble small houses or cells. Each of these small cells can hold only one small piece of information or data. This small piece can be only either 0 or 1. As a matter of fact, it is called a bit. This means that a bit can have a value of either 0 or 1.
A regular application uses different variables. Each variable has its own value. As variables are different, each variable must use or occupy its own area in the computer memory; also, variables use different values, and different types of values. Therefore, two different variables may require different amounts of storage in the computer memory. This means that one variable may require a certain number of bits while another variable uses a different number of bits fewer bits or more bits).
The size of the memory area that a variable requires depends on its type of value. This means that the number of bits that a variable needs depends on its type.
An Inferred Language
F# is an inferred language. This means that, when declaring a variable, you don't have to specify its data type but you must initialize it. When you do this, the compiler will "infer" the data type; that is, the compiler will decide about the appropriate type to apply to the variable.
As a result, you can omit specifying the data type of a variable and let the compiler take care of it, or you can specify it. If you decide to specify the data type of a variable, follow its name with a colon and the data type. The formula to follow is:
let variable-name : data-type
Introduction to Strings
Introduction
A string is a group of characters or symbols considered as one entity. To create a string, include its value in double-quotes.
A Type for Strings
A string is represented by a data type named string. Therefore, when declaring a variable, if you want to indicate that it is a string, use the string data type. Here is an example:
let message : string = "Welcome to the wonderful world of functional programming!"
When declaring a string variable, you can initialize it with an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes.
Displaying the Value of a String
To display the value of a string variable, somewhere in the double-quotes of a printf or printfn function, type %s as the placeholder of the variable. Of course type the name of the variable after the double-quotes. Here is an example:
let message = "Welcome to the wonderful world of functional programming!" printfn "%s" message
Practical Learning: Creating and Using Strings
let category = "1"; let gender = "M"; let employee = "Garçon Hérand"; printfn "Great Corporation"; printfn "Employee: %s" employee; printfn "Gender: %s" gender; printfn "Status: %s" category;
Great Corporation Employee: Garçon Hérand Gender M Status: 1 Press any key to close this window . . .
Introductory Operations on Strings
Sectioning a String
If you have a long string that you want to span on more than one line, cut its sections with a back slash each (although the second line in the following example is indented, you don't have to indent it). Here is an example:
let message = "Welcome to the wonderful world \
of functional programming!"
printfn "%s" message
There are many other issues related to strings. We cannot review them now.
Concatenation
String concatenation consists of adding one string to another. This is done using the addition operation. Here is an example:
let userName = "pjacobson"
let emailDomain = "@yonson.com"
let emailAddress = userName + emailDomain
printfn "Employee Email: %s" emailAddress
This would produce:
Employee Email: pjacobson@yonson.com
In the same way, you can use as many addition operators as necessary to concatenatte strings.
Practical Learning: Concatenating Strings
let firstName = "Philippe" let lastName = "Jacobson" let fullName = firstName + " " + lastName printfn "Employee Name: %s" fullName
Employee Name: Philippe Jacobson Press any key to close this window . . .
String Interpolation
String interpolation consists of adding placeholders in a string so that those placeholders represent some values. To create string interpolation, start the string with the $ symbol. Inside the double-quotes of the string, include the name of a variable whose value you will want to represent or display. That name must be included between { and } Here is an example:
let firstName = "Michael"; let lastName = "Carlock"; let employeeName = $"Full Name: {firstName} {lastName}" printfn "%s" employeeName
This would produce:
Full Name: Michael Carlock Press any key to close this window . . .
Characters
A character is a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z), a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), or a symbol that can be visually represented and recognized by the computer. Examples of symbols are: ` ~ ! @ # $ % ^ & * ( ) - _ = + [ ] \ | ; : ' < ? . / , > "
To use a value that represents a character, declare a variable and initialize with a letter, a digit, or a symbol in single-quotes. Here is an example:
let gender = 'F';
Displaying a Character
To display the value, in the double-quotes of printf or printfn, use %c as the placeholder of the variable. Here is an example:
let gender = 'F';
printf "Student Gender: ";
printfn "%c" gender;
This would produce:
Student Gender: M
The Type of a Character
When declaring the variable, to indicate that it is a character, use the char keyword. Here is an example:
let gender : char = 'F'
printf "Student Gender: "
printfn "%c" gender
An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of a line; that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, \, followed by another character or symbol. For example, the escape sequence that moves to the next line is \n.
An escape sequence can be included in the quotes of printf or printfn. The escape sequences are:
Escape Sequence | Name | Description |
\b | Backspace | Takes the cursor back |
\t | Horizontal Tab | Takes the cursor to the next tab stop |
\n | New line | Takes the cursor to the beginning of the next line |
\r | Carriage return | Causes a carriage return |
\" | Double Quote | Displays a quotation mark (") |
\' | Apostrophe | Displays an apostrophe (') |
\\ | Backslash | Displays a backslash (\) |
\0 | Null | Displays a null character |
\uXXXX | Unicode Character | Used to display the equivalent representation of a symbol given in hexadecimal format |
\UXXXXXXXX | Unicode Character | Used to display the equivalent representation of a symbol given in hexadecimal format |
Here is an example:
let gender : char = 'M';
printf "Student Gender: ";
printfn "%c\n" gender;
A String, Verbatim
Consider the following string:
let filePath = "C:\ne -> Neon\ti -> Titanium\be -> Beryllium\v -> Vanadium"
printfn "File Path: %s" filePath
This would produce:
File Path: C: e -> Neon i -> Titaniue -> Beryllium♂ -> Vanadium Press any key to close this window . . .
As we have seen in previous sections and we will see in future sections, you can include some operators in a string to perform local operations (operations inside a string). The compiler will refer to the symbols you include in a string in order to perform the desired operation(s). In some cases, you will want the compiler to ignore the operators in a string and consider the string "as is", as if those symbols are part of the string and should not be processed as operators. Such a string is referred to as "verbatim".
To create a verbatim string, start it with the @ symbol. Here is an example:
let filePath = @"C:\ne -> Neon\ti -> Titanium\be -> Beryllium\v -> Vanadium"
printfn "File Path: %s" filePath
This would produce:
File Path: C:\ne -> Neon\ti -> Titanium\be -> Beryllium\v -> Vanadium Press any key to close this window . . .
A String with Triple Quotation
A string can be included in another string. To make this happen, you want the included string to have its own double-quotes. To do this, create each inside double-quote with the \" escape sequence. Here are examples:
let chemistry = "H: \"Hydrogen\"\nHe: \"Helium\"\nLi: \"Lithium\"\nBe: \"Beryllium\"" printfn "Chemistry\n%s" chemistry
This would produce:
Chemistry H: "Hydrogen" He: "Helium" Li: "Lithium" Be: "Beryllium"
On the other hand, if you precede a string value with the @ sign, all escape sequences in the string would be ignored, except for the double-quotes. If you want the double-quotes to be ignored, start and end the string with three double-quotes. In this case, each included double-quote doesn't need to be created a \" escape sequence. Here is an example:
let filePath = """C:\ne -> Neon\ti -> Titanium\be -> Beryllium\v -> Vanadium"""
printfn "File Path: %s" filePath
This would produce:
File Path: C:\ne -> Neon\ti -> Titanium\be -> Beryllium\v -> Vanadium Press any key to close this window . . .
Introduction to Integral Values
Introduction to Bytes
A byte is a combination of 8 bits. As a result, this combination of 8 bits, referred to as a byte, can be used for a variable that needs a small number, in the area of 256.
A number is referred to as signed if it can hold a negative of a positive value in the 256 range. If you divide this number in half and consider its negative and positive side, you get values from -128 to 127. As a result, a variable for a byte can hold a number between -128 to 127.
Because you can declare a variable without specifying its type, to indicate that you want the variable to store its value in a computer memory equivalent to a byte, when initializing the variable, add y to its value.
A variable is referred to as signed if it can hold a negative or a positive number. If the number is positive, you can start it with a + sign. Here is an example:
let age = 14y
Actually, the + symbol is optional. This means that if you don't use it, the compiler will automatically consider that the number is positive. On the other hand, if the number is negative, you must start it with a - sign. Here is an example:
let temperature = -104y
Presenting the Value of an Integral Variable
To represent the value of a signed byte variable, use either %d or %i in the double-quotes of the printf or printfn. Here is an example:
let roomTemperature = -88y;
printfn "When we entered, the room temperature was %d" roomTemperature;
This would produce:
When we entered, the room temperature was -88
As seen already, a variable is referred to as signed if it can hold a number between -127 and 128. The data type of such a variable is named sbyte.Therefore, when declaring a variable for a small signed number, if you want to specify its type, use that keyword.
Practical Learning: Introducing Numbers
let number = 0xFE; printfn "Number: %d" number;
Number: 254 Press any key to close this window . . .
let roomTemperature : sbyte = -88y;
printfn "When we entered, the room temperature was %d" roomTemperature;
A Byte
Introduction
A number is referred to as unsigned if it doesn' have a sign. As see in our introduction to integral types, a number that doesn't have a signed is automatically considered positive. In fact, in application development, when a variable is said to be unsigned, it means it always holds a positive number.
The Byte Type
We saw that you may want to use a variable whose value can fit in 8 bits, equivalent to a byte. In some cases, you want the value of that variable to also be positive. Such a value must be between 0 and 255. Indicate this, when declaring the variable and initialing it, add uy to its value. Here is an example:
let age:byte = 14uy;
Make initializing the variable, make sure you don't use a value that is higher than 255; you would receive an error.
To let you specify the data type of an unsigned variable that must hold small numbers that can fit in 8 bits, the F# language provides the byte keyword. You can apply to when declaring the variable. Here is an example:
let age :byte = 14uy
As seen with signed integers, to display a signed variable, in the double-quotes of printf or of printfn, use %d or %i. Here is an example:
let age:byte = 14uy;
printf "Student Age: "
printfn "%d" age
Short Integers
A short integer is number that can fit in 2 bytes, which is equivalent to 2 * 8 bits = 16 bits.
A number is referred to as a signed short integer it can be a negative or a positive number that can fit in 16 bits. This means that the number is between -32768 to 32767. Here is an example of declaring such a variable:
let schoolEffective = 1400
When declaring and initialing the variable, to indicate that you it to hold a value equivalent to a signed short integer, end its number with s. Here is an example:
let schoolEffective = 2247s
To support signed short integers, the F# language provides the int16 keyword. You can use it to specify the data type of the variable.
As seen above, to represent a signed short variable in the double-quotes of printf of printfn, use %d or %i. Here is an example:
let schoolEffective = 1400s printfn "School Effective: %d" schoolEffective
This would produce:
School Effective: 1400 Press any key to close this window . . .
Practical Learning: Introducing Short Integers
let numberOfPages : int16 = 842s let temperature : int16 = -1544s printfn "Number of Pages of the book: %d" numberOfPages printfn "Temperature to reach during the experiment: %d degrees\n" temperature
Number of Pages of the book: 842 Temperature to reach during the experiment: -1544 degrees Press any key to close this window . . .
If a variable must hold positive and relatively small numbers, it is referred to as an unsigned short integer. To use such a variable, when initializing it, add a us suffix to its value. Here are examples:
let numberOfTracks = 16us let musicCategory = 2us printfn "This music album contains %d tracks" numberOfTracks printfn "Music Category: %d" musicCategory
This would produce:
This music album contains 16 tracks Music Category: 2 Press any key to close this window . . .
An unsigned short integer can hold numbers that range from 0 to 65535 and therefore can fit in 16 bits. If you want to indicate the data type of the variable, use uint16. Here are examples:
let numberOfTracks:uint16 = 16us let musicCategory:uint16 = 2us printfn "This music album contains %d tracks" numberOfTracks printfn "Music Category: %d" musicCategory
Integers
Introduction
An integer is a natural number. By default, the number can fit in 32 bits, which means it can use 32 / 8 = 4 bytes.
A natural number is said to be signed if it can be negative or positive. Such a number is between 2,147,483,648 and 2,147,484,647. When declaring a variable and intializing it with a natural number, the compilter automatically considers that you can want to use 32 bits to store the number, regardless of the value you assign to the variable. Here are two examples of such variables:
let coordX = 12 let coordY = 8
As seen with the other integers, to display the value of an integral variable, in the double-quotes of printf of printfn, use %d or %i. Here is an example:
let coordX = 12 let coordY = 8 printfn "Cartesian Coordinate System: P(%d, %d)" coordX coordY
When executed, the program would produce:
Cartesian Coordinate System: P(12, 8)
An Integral Type
Remember that, when declaring a variable that will hold a natural number, simply assign the desired number to it and the compiler will consider it a number targetting 32 bits. To explicitly indicate it to the compiler, you can add l to the value of the variable. Here is an example:
let number = 183058l printfn "Number: %i" number
To support natural numbers, the F# language provides a data type named int. You can apply it to a variable.
An unsigned integer is a positive number between 0 to 4,294,967,295. When declaring a variable, to indicate that you want it to hold only positive integers, add u or ul to its value. Here are examples:
let dayOfBirth = 8u;
let monthOfBirth = 11u
let yearOfBirth = 1996u
To support unsigned integers, the F# language provides the uint32 data type. Here are examples:
let dayOfBirth : uint32 = 8u let monthOfBirth : uint32 = 11u let yearOfBirth : uint32 = 1996u printfn "Red Oak High School"; printfn "Student Date of Birth: %d/%d/%d" monthOfBirth dayOfBirth yearOfBirth;
This would produce:
Red Oak High School Student Date of Birth: 11/8/1996 Press any key to close this window . . .
Long Integers
Introduction
A long integer is a very large natural number. It can fit in 64 bits.
A Signed Long Integer
A signed long integer is a natural number that can be very low or very large. Here is an example:
let population = 72394475;
printfn "Country Population: %d" population;
This would produce:
Country Population: 72394475 Press any key to close this window . . .
When initializing the variable, to indicate that it represents a long integer, add L to its value. Here is an example:
let population = 72394475L
printfn "Country Population: %d" population
When initialing a variable, to indicate that you want it to hold signed long integers, add L to its value. To let you specify the data type of the variable, the F# language provides the int64 keyword. Here is an example:
let population : int64 = 72394475L
An unsigned long integer is a very large positive number. To support those types of values, the F# language provides the uint64 data type. When initializing the variable, you can add UL to its value.
If you want your variable to hold a value that requires more than 64 bit, the F# language provides the bigint data type. When initializing the variable, you can add I to its value.
Native Pointer Integers
Native Integers
You can declare a variable that will hold values as a native integer. When initializing the variable, add an n suffix to its value. To display its value, use %d. Here is an example:
let number : nativeint = 16n; printfn "Number = %d" number;
This would produce:
Number = 16 Press any key to close this window . . .
Unsigned Native Integers
To declare a variable for an unsigned native integer, when initializing the variable, add a un suffix to its value. To display its value, use %d. Here is an example:
let number : unativeint = 16un printfn "Number = %d" number
Options on Integers
Instead of a natural number, you can also initialize an integral variable with a hexadecimal value. To do this, when initialing the variable, start its value with 0x and add a combination of 0, 1, 2, 3, 4, 5, 6, a (or A), b (or B), c (or C), d (or D), e (or E), and f (or F). Here is an example:
let number = 0xF0488EA;
printfn "Number: %d" number;
This would produce:
Number: 251955434 Press any key to close this window . . .
If you want to control the amount of memory area that the variable will use, the combination of those digits and/or characters must produce a value appropriate for the type you want. For example, if the value is for a byte, make sure the combination must produce a value equivalent to, or less than, 255. Here is an example:
let number = 0xFE printfn "Number: %d" number
This would produce:
Number: 254 Press any key to close this window . . .
If you want, you can represent a number as octal. To do this, start the number with 0o, then add a combination of 0, 1, 2, 3, 4, 5, 6, and/or 7 digits in your desired order. Here is an example:
let number = 0o2027146
printfn "Number: %d" number
This would produce:
Number: 66662 Press any key to close this window . . .
If you want, you can represent a number as binary. You start the number with 0b. After that, add a combination of 0s and 1s in the order of your choice. Here is an example:
let number = 0b1110111011
printfn "Number: %d" number
This would produce:
Number: 955 Press any key to close this window . . .
Consider Thousands
When declaring a variable, if its value is in the thousands, you can segments from the right side with underscores. Here are examples:
let nbr1 = -38y let nbr2 = +5_252s let nbr3 = -846_084_325 let nbr4 = 69_249_484 let number1 = 0xFE_a8 let number2 = 0xfe_Bb_6C printfn "Number: %i" nbr1 printfn "Number: %i" nbr2 printfn "Number: %i" nbr3 printfn "Number: %i" nbr4 printfn "Number: %i" number1 printfn "Number: %i" number2
This would produce:
Number: -38 Number: 5252 Number: -846084325 Number: 69249484 Number: 65192 Number: 4111212 Press any key to close this window . . .
Practical Learning: Introducing Numbers
A real number is a number that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period(.).
On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines the precision of the number.
F# supports floating-point numbers that range from ±1.5 — 10−45 to ±3.4 — 1038 with a precision of 7 digits that can fit in 32 bits. To declare such a variable, add f or F to its value. Here is an example:
let distance = 12f;
To indicate the data type of the variable, you can use either the single or the float32 data type. Here is an example:
let distance : single = 12f;
When a variable is larger than the single type can handle and requires more precision, you should declare it using either the float or the double keyword. Here is an example:
let number = 62834.9023;
A variable declared as double stores very large numbers ranging from ±5.0 x 10−324 to ±1.7 x 10308 with a precision of 15 or 16 digits.
To display the value of variable that holds a floating-point number, add the f character after the % format in the double-quotes of printf or printfn. Here is an example:
let number = 62834.968273; printfn "Number: %f" number;
This would produce:
umber: 62834.968273 Press any key to close this window . . .
As seen in our introduction, a floating-point number has two parts, one on each side of the decimal separator. The part on the right side of the decimal separator can have many digits. In some cases, you may want to limit the number of digits after the decimal separator. To do this, between the % symbol and f, type 0. and the desired number of digits. Here is an example:
let number = 62834.968273;
printfn "Number: %0.3f" number;
This would produce:
Number: 62834.968 Press any key to close this window . . .
Actually, you can omit the 0 and use the period directly after the % symbol. Here is an example:
let number = 62834.92682736;
printfn "Number: %.5f" number;
This would produce:
Number: 62834.92682 Press any key to close this window . . .
Controlling the Space of a Floating Number
When you display the value of a variable that holds a floating-point number, you can specify the amount of space to leave between the number of the last character on its left. To do that, before the period of the % and f symbols, type the deside number. Here is an example:
let number = 62834.968273
printfn "Number: %0.3f" number
printfn "Number: %16.3f" number
This would produce:
Number: 62834.968 Number: 62834.968 Press any key to close this window . . .
The decimal data type can be used to declare a variable that would hold significantly large values. You declare such a variable using the decimal keyword. The values stored in a decimal variable can range from ±1.0 — 10−28 to ±7.9 — 1028 with a precision of 28 to 29 digits.
After declaring a decimal variable, you can initialize it with a natural number. To indicate that the variable holds a decimal value, when initializing it, add an M suffix to its value. Here is an example:
let hourlySalary: decimal = 24.25M; printfn "Hourly Salary = %f" hourlySalary;
This would produce:
Hourly Salary = 24 Press any key to close this window . . .
As seen in previous sections and this one, when declaring and initializing a real variable, the suffix you give to its assigned value indicates to the compiler the actual type of value and the type of memory that would be allocated for the variable.
The Infinity Concept
Infinity is the concept to consider the highest decimal number that could exist. That number doesn't actually exist but, to support the idea, the F# language provides the infinity keyword. Here is an example of accessing it:
let population = infinity; printfn "Population: %.3f" population
This would produce:
Population: Infinity Press any key to close this window . . .
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2009-2024, FunctionX | Sunday 10 September 2023 | Next |
|