When you want to learn a new programming language, it's good to focus on the things programming languages have in common:
- Variables
- Expressions
- Statements
These concepts are the basis of most programming languages. Once you understand them, you can start figuring out the rest. Because programming languages usually share similarities, once you know one language, you can learn the basics of another by understanding its differences.
A good way to learn new languages is practicing with a standard program. This allows you to focus on the language, not the program's logic. I'm doing that in this article series using a "guess the number" program, in which the computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the number correctly.
This program exercises several concepts in programming languages:
- Variables
- Input
- Output
- Conditional evaluation
- Loops
It's a great practical experiment to learn a new programming language.
Install Ada
The Ada programming language is a unique and highly structured language with a dedicated developer base. The toolchain for Ada is the GNU Ada Development Environment, better known as GNAT.
You can install GNAT on Linux using your distribution's package manager. On Fedora, CentOS, or similar:
$ sudo dnf install gcc-gnat
On Debian, Linux Mint, and derivatives:
$ sudo apt install gnat
On macOS and Windows, you can download an installer from the Adacore website (choose your platform from the drop-down menu).
Guess the number in Ada
Create a file called game.adb
.
The two built-in Ada libraries this program uses are Text_IO
and Numerics.Discrete_Random
:
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
Procedure head
The name of the procedure must match the name of the file. The first part is defining the variables.
Note that the discrete_random
is specialized to a specific range. In this case, the range of numbers allowed:
procedure Game is
type randRange is range 1..100;
package Rand_Int is new ada.numerics.discrete_random(randRange);
use Rand_Int;
gen : Generator;
num : randRange;
incorrect: Boolean := True;
guess: randRange;
Procedure logic
The logic starts by reset(gen)
. This initializes the random number generator, ensuring the number, initialized with random(gen)
, will be different each time you run the program.
The next step is to run the loop:
- Output the instructions for a guess
- Read the line
- Convert it to
randRange
- Check it against the number
If the number matches, incorrect is set to False, causing the next iteration of the loop to exit.
Finally, the program prints a confirmation of the guess correctness before exiting:
begin
reset(gen);
num := random(gen);
while incorrect loop
Put_Line ("Guess a number between 1 and 100");
declare
guess_str : String := Get_Line (Current_Input);
begin
guess := randRange'Value (guess_str);
end;
if guess < num then
Put_line("Too low");
elsif guess > num then
Put_line("Too high");
else
incorrect := False;
end if;
end loop;
Put_line("That's right");
end Game;
Build the program
The easiest way to compile an Ada program is to use gnatmake
:
$ gnatmake game.adb
aarch64-linux-gnu-gcc-10 -c game.adb
aarch64-linux-gnu-gnatbind-10 -x game.ali
aarch64-linux-gnu-gnatlink-10 game.ali
This generates a binary called game
.
Run the program
Each run of the program will be a little different. This is one example:
$ ./game
Guess a number between 1 and 100
50
Too low
Guess a number between 1 and 100
75
Too low
Guess a number between 1 and 100
82
Too low
Guess a number between 1 and 100
90
Too high
Guess a number between 1 and 100
87
Too low
Guess a number between 1 and 100
88
That's right
Learn Ada
This "guess the number" game is a great introductory program for learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of the languages and compare their details.
Do you have a favorite programming language? How would you write the "guess the number" game in it? Follow this article series to see examples of other programming languages that might interest you!
Comments are closed.