comp.lang.ada
 help / color / mirror / Atom feed
From: Mike H <postmaster@ada-augusta.demon.co.uk>
Subject: Re: need help learning Ada for a modula-2 programmer
Date: Thu, 30 Jan 2014 14:20:48 +0000
Date: 2014-01-30T14:20:48+00:00	[thread overview]
Message-ID: <ZiGSOnCA$l6SFwTY@ada-augusta.demon.co.uk> (raw)
In-Reply-To: 878utybjxc.fsf@adaheads.sparre-andersen.dk

In message <878utybjxc.fsf@adaheads.sparre-andersen.dk>, Jacob Sparre 
Andersen <jacob@jacob-sparre.dk> writes
>Generalising the array index beyond type Integer takes up ten lines in
>my first take on the task.  (Recursion _might_ make it shorter.)
>
-- ===================================================
-- Below is the 'body' of a generic function whose formal specification 
is ...
--
-- generic
--    type Item is private;
--    type Index is (<>);
--    type Array_type is array (index range <>)of Item;
-- function Shift_right_circular
--    (A     : Array_type;
--    Places : Natural) return Array_type;
--
-- ===================================================
--
function Shift_right_circular
   (A      : Array_type;
    Places : Natural) return Array_type is

    -- Split A into two slices A(I1 .. I2) & A(I3 .. I4). The position
    -- of the split is between I2 and I3. I3 is offset from I1 by 
distance P.
    -- I2 is the predecessor of I3.
    P      : Natural := Places rem A'LENGTH;
    I2, I3 : Index; -- Note: type of Index will be known from the 
instantiation

begin

    if P > 0 then
       -- The two slices of A are rejoined as (I3 .. I4) & (I1 .. I2).
       P  := Index'POS(A'FIRST) + P;
       I3 := Index'VAL(P);
       I2 := Index'PRED(I3);
       return A(I3 .. A'LAST) & A(A'FIRST .. I2);
    else
         -- P = 0 is an anomalous case (I3 has no predecessor so the line
         -- "I2 := Index'PRED(I3)" would raise a constraint error), so 
....
       return A;
    end if;

end Shift_right_circular;

--
In PLAN (ICL 1900 assembler) the mnemonic was SRC
The thing I like best about the Internet is that no one
knows that, in reality, I am just another old dog!
Rightpondian Mike

  parent reply	other threads:[~2014-01-30 14:20 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-28  1:06 need help learning Ada for a modula-2 programmer agent
2014-01-28  1:33 ` adambeneschan
2014-01-28  1:52 ` Jeffrey Carter
2014-01-28 12:18 ` Brian Drummond
2014-02-02  2:47   ` agent
2014-02-02  6:09     ` Jeffrey Carter
2014-02-02 15:02       ` agent
2014-02-02 16:00         ` gautier_niouzes
2014-02-02 19:48         ` Jeffrey Carter
2014-02-03  8:24           ` Dmitry A. Kazakov
2014-02-02 17:18     ` Brian Drummond
2014-02-03  0:10       ` agent
2014-02-03  0:36         ` agent
2014-02-03 12:53         ` Brian Drummond
2014-01-28 22:51 ` Jerry
2014-01-29 12:15   ` Mike H
2014-01-29 20:41     ` Jacob Sparre Andersen
2014-01-29 23:52       ` Jeffrey Carter
2014-01-30  9:05         ` Jacob Sparre Andersen
2014-01-30 14:20       ` Mike H [this message]
2014-01-30 14:35         ` Bill Findlay
2014-01-30 15:40           ` Mike H
2014-01-30 23:39         ` Jeffrey Carter
2014-01-31 20:16           ` Mike H
2014-01-29 23:52     ` Jeffrey Carter
2014-01-30  1:44       ` Bill Findlay
2014-01-30  2:01         ` Jeffrey Carter
2014-01-30 12:24       ` Simon Wright
2014-01-30 23:38         ` Jeffrey Carter
2014-02-03 23:12     ` agent
2014-02-04  6:10       ` J-P. Rosen
2014-02-04 22:38   ` agent
2014-01-29 16:58 ` Dirk Heinrichs
2014-01-29 20:43   ` Randy Brukardt
2014-01-29 22:53     ` Georg Bauhaus
2014-01-30 12:13       ` Simon Wright
2014-01-30 17:05     ` Dirk Heinrichs
2014-01-30 23:21       ` Randy Brukardt
2014-01-30  4:29   ` Nasser M. Abbasi
2014-01-30  8:45     ` Where to put change descriptions (Was: need help learning Ada for a modula-2 programmer) Jacob Sparre Andersen
2014-01-30  9:53     ` need help learning Ada for a modula-2 programmer Georg Bauhaus
2014-01-30 21:58       ` Randy Brukardt
2014-01-30 16:28     ` Pascal Obry
2014-01-30 17:43       ` Marius Amado-Alves
2014-01-30 18:10       ` Simon Wright
2014-01-30 22:38       ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox