diff --git a/courses/fundamentals_of_ada/070_subprograms.rst b/courses/fundamentals_of_ada/070_subprograms.rst index 01c237bdb..4d7c8b011 100644 --- a/courses/fundamentals_of_ada/070_subprograms.rst +++ b/courses/fundamentals_of_ada/070_subprograms.rst @@ -42,6 +42,6 @@ Subprograms .. include:: 070_subprograms/07-function_specifics.rst .. include:: 070_subprograms/08-expression_functions.rst .. include:: 070_subprograms/09-potential_pitfalls.rst -.. include:: 070_subprograms/10-extended_examples.rst +.. include:: 070_subprograms/10-extended_example.rst .. include:: labs/070_subprograms.lab.rst .. include:: 070_subprograms/99-summary.rst diff --git a/courses/fundamentals_of_ada/070_subprograms/10-extended_example.rst b/courses/fundamentals_of_ada/070_subprograms/10-extended_example.rst new file mode 100644 index 000000000..6fe8e03df --- /dev/null +++ b/courses/fundamentals_of_ada/070_subprograms/10-extended_example.rst @@ -0,0 +1,144 @@ +=================== +Extended Example +=================== + +----------------------------- +Implementing a Simple "Set" +----------------------------- + +* We want to indicate which colors of the rainbow are in a **set** + + * If you remember from the *Basic Types* module, a type is made up of values and primitive operations + +* Our values will be + + * Type indicating colors of the rainbow + * Type to group colors + * Mechanism to indicate which color is in our set + +* Our primitive operations will be + + * Create a set + * Add a color to the set + * Remove a color from the set + * Check if color is in set + +-------------------- +Values for the Set +-------------------- + +* Colors of the rainbow + + .. code:: Ada + + type Color_T is (Red, Orange, Yellow, Green, + Blue, Indigo, Violet, + White, Black); + +* Group of colors + + .. code:: Ada + + type Group_Of_Colors_T is + array (Positive range <>) of Color_T; + +* Mechanism indicating which color is in the set + + .. code:: Ada + + type Set_T is array (Color_T) of Boolean; + -- if array component at Color is True, + -- the color is in the set + +---------------------------------- +Primitive Operations for the Set +---------------------------------- + +* Create a set + + .. code:: Ada + + function Make (Colors : Group_Of_Colors_T) return Set_T; + +* Add a color to the set + + .. code:: Ada + + procedure Add (Set : in out Set_T; + Color : Color_T); + +* Remove a color from the set + + .. code:: Ada + + procedure Remove (Set : in out Set_T; + Color : Color_T); + +* Check if color is in set + + .. code:: Ada + + function Contains (Set : Set_T; + Color : Color_T) + return Boolean; + +-------------------------------------------- +Implementation of the Primitive Operations +-------------------------------------------- + +* Implementation of the primitives is easy + + * We could do operations directly on :ada:`Set_T`, but that's not flexible + +.. code:: Ada + + function Make (Colors : Group_Of_Colors_T) return Set_T is + Set : Set_T := (others => False); + begin + for Color of Colors loop + Set (Color) := True; + end loop; + return Set; + end Make; + + procedure Add (Set : in out Set_T; + Color : Color_T) is + begin + Set (Color) := True; + end Add; + + procedure Remove (Set : in out Set_T; + Color : Color_T) is + begin + Set (Color) := False; + end Remove; + + function Contains (Set : Set_T; + Color : Color_T) + return Boolean is + (Set (Color)); + +------------------------- +Using our Set Construct +------------------------- + +.. code:: Ada + + Rgb : Set_T := Make ((Red, Green, Blue)); + Light : Set_T := Make ((Red, Yellow, Green)); + +.. code:: Ada + + if Contains (Rgb, Black) then + Remove (Rgb, Black); + else + Add (Rgb, Black); + end if; + +*In addition, because of the operations available to arrays of Boolean, we can easily implement set operations* + +.. code:: Ada + + Union : Set_T := Rgb or Light; + Intersection : Set_T := Rgb and Light; + Difference : Set_T := Rgb xor Light; diff --git a/courses/fundamentals_of_ada/070_subprograms/10-extended_examples.rst b/courses/fundamentals_of_ada/070_subprograms/10-extended_examples.rst deleted file mode 100644 index fe0ce6a49..000000000 --- a/courses/fundamentals_of_ada/070_subprograms/10-extended_examples.rst +++ /dev/null @@ -1,137 +0,0 @@ -=================== -Extended Examples -=================== - ------------------------------------- -Tic-Tac-Toe Winners Example (Spec) ------------------------------------- - -.. container:: columns - - .. container:: column - - .. code:: Ada - - package TicTacToe is - type Players is (Nobody, X, O); - type Move is range 1 .. 9; - type Game is array (Move) of - Players; - function Winner (This : Game) - return Players; - ... - end TicTacToe; - - .. container:: column - - .. list-table:: - - * - :subscript:`1` N - - - :subscript:`2` N - - :subscript:`3` N - - * - :subscript:`4` N - - - :subscript:`5` N - - :subscript:`6` N - - * - :subscript:`7` N - - - :subscript:`8` N - - :subscript:`9` N - ------------------------------------- -Tic-Tac-Toe Winners Example (Body) ------------------------------------- - -.. code:: Ada - - function Winner (This : Game) return Players is - type Winning_Combinations is range 1 .. 8; - type Required_Positions is range 1 .. 3; - Winning : constant array - (Winning_Combinations, Required_Positions) - of Move := (-- rows - (1, 2, 3), (4, 5, 6), (7, 8, 9), - -- columns - (1, 4, 7), (2, 5, 8), (3, 6, 9), - -- diagonals - (1, 5, 9), (3, 5, 7)); - - begin - for K in Winning_Combinations loop - if This (Winning (K, 1)) /= Nobody and then - (This (Winning (K, 1)) = This (Winning (K, 2)) and - This (Winning (K, 2)) = This (Winning (K, 3))) - then - return This (Winning (K, 1)); - end if; - end loop; - return Nobody; - end Winner; - -------------- -Set Example -------------- - -.. code:: Ada - - -- some colors - type Color is (Red, Orange, Yellow, Green, Blue, Violet); - -- truth table for each color - type Set is array (Color) of Boolean; - -- unconstrained array of colors - type Set_Literal is array (Positive range <>) of Color; - - -- Take an array of colors and set table value to True - -- for each color in the array - function Make (Values : Set_Literal) return Set; - -- Take a color and return table with color value set to true - function Make (Base : Color) return Set; - -- Return True if the color has the truth value set - function Is_Member (C : Color; Of_Set: Set) return Boolean; - - Null_Set : constant Set := (Set'Range => False); - RGB : Set := Make ( - Set_Literal'(Red, Blue, Green)); - Domain : Set := Make (Green); - - if Is_Member (Red, Of_Set => RGB) then ... - - -- Type supports operations via Boolean operations, - -- as Set is a one-dimensional array of Boolean - S1, S2 : Set := Make (....); - Union : Set := S1 or S2; - Intersection : Set := S1 and S2; - Difference : Set := S1 xor S2; - ------------------------------- -Set Example (Implementation) ------------------------------- - -.. code:: Ada - - function Make (Base : Color) return Set is - Result : Set := Null_Set; - begin - Result (Base) := True; - return Result; - end Make; - - function Make (Values : Set_Literal) return Set is - Result : Set := Null_Set; - begin - for K in Values'Range loop - Result (Values (K)) := True; - end loop; - return Result; - end Make; - - function Is_Member (C: Color; - Of_Set: Set) - return Boolean is - begin - return Of_Set (C); - end Is_Member; -