:: Re: [DNG] Learning C (books)
Kezdőlap
Delete this message
Reply to this message
Szerző: Didier Kryn
Dátum:  
Címzett: dng
Új témák: [DNG] Ada example (was: Re: Learning C (books))
Tárgy: Re: [DNG] Learning C (books)
Le 05/10/2024 à 08:51, Steve Litt a écrit :
> Would you be able to give a presentation of Ada at the December 2024
> online GoLUG meeting? It's 7PM Eastern Time (probably standard time by
> then) on the first Wednesday in December. We're on a run of having a
> lot of interesting presentations.


    Here is an example of a very small program I wrote many years ago
before a travel to Japan. You will easily understand what it does, but I
will explain line by line the source. The file is named jetlag.adb

 1 with Ada.Text_Io;
 2 use Ada.Text_Io;
 3
 4 procedure Jetlag is
 5    type H24_T is mod 24;
 6    package H24_Io is new Modular_Io ( H24_T );
 7    use H24_Io;
 8    J: H24_T;
 9 begin
10    Put ("France: ");
11    for T in H24_T loop
12       Put ( Item=>T, Width=>2 );
13       Put ("h ");
14    end loop;
15    New_Line;
16    Put ("Japon:  ");
17    for T in H24_T loop
18       J := T+8;
19       Put ( Item=>J, Width=>2 );
20       Put ("h ");
21    end loop;
22 end Jetlag;

line 1 is kinda #include in C, it declares we will refer to the
declarations of package Ada.Text_Io . We will see that this package
contains the declaration of the child package Ada.Text_Io.Modular_Io.

Line 2is a namespace instruction: if there is a Thing declared in
package Ada.Text_Io, we do not need to call it Ada.Text_Io.Thing, but
just Thing.

Line 4 starts the declaration of a procedure. Lines 5-8 are
declarations. After line 9 are the instruction, and line 22 is the end
of our procedure, which is the main program.

line 5 declares a modular type, an integer number ranging from 0 to 23.

line 6 declares an instantiation of the generic child package Modular_Io
for the type we have declared

line 7: thanks to this line, we don't need to write H24_Io.Put but just
Put in lines 12, and 19

line 8 declares a variable of type H24_T. Note that such a discrete type
is also a set,

line 10 invokes the procedure Ada.Text_Io.Put, with a string as unique
argument. Remark: arguments to subprograms may be ommitted when they
have default values.

line 11: describes a loop with the running index T of type H24_T, that
is belonging to the H24_T set. We could run it backward by writing "for
T in reverse H24_T".

line 12 prints the value of T using two characters (a space is inserted
if only one digit is needed). It calls the procedure H24_Io.Put with the
syntax of designated arguments: Item is the value to print and Space is
the size of the printed text. There may be other arguments, such as
Base, if you want to print it  eg, in octal.

line 14 is obvious

line 15 invokes the procedure Ada.Text_Io.New_Line. If you invoke a
procedure without argument, you don't write parentheses

line 18: the addition of modular numbers performs the wrapping
automatically.

line 22: end of procedure JetLag.

To compile: 'sudo apt-get install gnat && gnatmake jetlag && ./jetlag'