From: Yves Bailly <kafka.fr@laposte.net>
Subject: Re: Char type verification
Date: Fri, 17 Nov 2006 00:30:51 +0100
Date: 2006-11-17T00:32:09+01:00 [thread overview]
Message-ID: <455cf4f8$0$17901$426a74cc@news.free.fr> (raw)
In-Reply-To: D4S6h.146805$aJ.102967@attbi_s21
Jeffrey R. Carter wrote:
>> It was intended as "How do you translate this example to Ada? How would
>> you, as a presumably experienced Ada coder, do it? What hoops would we
>> jump through?
>
> What this example does is output whether the 1st character of the 1st
> command-line argument is in the range 'A' .. 'Z'. In the Ada world, we
> tend to ignore the details of badly designed examples in poorly designed
> languages and simply implement the same functionality. Since there is no
> reason for your conversions and home-grown function, we're not going to
> waste effort translating them.
I'm very sorry Jeffrey, but I'm afraid you're missing the point. The given
example is no more than what it is : an example. There are plenty of ways
to know if a character is upper-case or not (what about the procedure
Is_Upper in the package Ada.Characters.Handling, by the way? did I missed
it in the replies?), and I'm quite sure Koray would have found most of them,
if not all (assuming it's possible).
As I understand it, the real question was : "having a list of contiguous
values of some type, how to know if a given value of that same type is
contained into the list". It's a kind of basic hashing: you can assume
to always be able to map your type's values to integers. So you can
replace "list of contiguous values" by "range of values".
So, here's my modest contribution. Don't bother to say it's overcomplex.
First create a generic package:
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
generic
type T is private;
type H is range <>;
with function Hash(val: in T) return H;
package P is
function Is_In_Range(lower: in T;
upper: in T;
value: in T)
return Boolean;
end P;
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
The body performs the actual check of validity:
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
package body P is
function Is_In_Range(lower: in T;
upper: in T;
value: in T)
return Boolean is
begin
return Hash(value) in Hash(lower)..Hash(upper);
end Is_In_Range;
end P;
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
And now use it in a test program:
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with P;
procedure Test is
function To_Natural(c: in Character)
return Natural is
begin
return Natural(Character'Pos(c));
end To_Natural;
package P_Char is new P(T => Character,
H => Natural,
Hash => To_Natural);
c: Character;
begin
c := Argument(1)(1);
Put("Is_Upper('" & c & "') = ");
Put_Line(Boolean'Image(P_Char.Is_In_Range('A', 'Z', c)));
end Test;
--8<-----8<-----8<-----8<-----8<-----8<-----8<---
Isn't Ada delightfull? :-)
Regards,
--
(o< | Yves Bailly : http://kafka-fr.net | -o)
//\ | Linux Dijon : http://www.coagul.org | //\
\_/ | | \_/`
next prev parent reply other threads:[~2006-11-16 23:30 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-15 22:00 Char type verification KE
2006-11-15 21:57 ` Georg Bauhaus
2006-11-15 23:15 ` KE
2006-11-16 4:48 ` Jeffrey R. Carter
2006-11-16 19:53 ` Adam Beneschan
2006-11-16 23:30 ` Yves Bailly [this message]
2006-11-17 0:48 ` Jeffrey R. Carter
2006-11-17 1:59 ` Adam Beneschan
2006-11-17 11:30 ` Stephen Leake
2006-11-17 15:33 ` KE
2006-11-17 15:10 ` Georg Bauhaus
2006-11-17 18:30 ` Ludovic Brenta
2006-11-18 2:29 ` Brian May
2006-11-17 19:45 ` Jeffrey R. Carter
[not found] ` <mQm7h.8782$ig4.3262@newsread2.news.pas.earthlink.net>
2006-11-17 19:56 ` Jeffrey R. Carter
[not found] ` <omz7h.222$1s6.165@newsread2.news.pas.earthlink.net>
2006-11-19 2:19 ` OT: French Idioms (was Re: Char type verification) Jeffrey R. Carter
2006-11-19 9:04 ` Dmitry A. Kazakov
2006-11-17 21:22 ` Char type verification Simon Wright
2006-11-17 23:59 ` Yves Bailly
2006-11-15 23:23 ` Simon Wright
2006-11-15 23:33 ` KE
2006-11-16 4:52 ` Jeffrey R. Carter
2006-11-15 23:36 ` Adam Beneschan
2006-11-15 23:55 ` KE
2006-11-16 4:54 ` Jeffrey R. Carter
2006-11-16 1:08 ` jimmaureenrogers
2006-11-16 1:45 ` KE
2006-11-16 2:15 ` jimmaureenrogers
2006-11-16 2:42 ` Steve Whalen
2006-11-16 9:36 ` Alex R. Mosteo
2006-11-16 7:02 ` KE
2006-11-16 17:04 ` Dmitry A. Kazakov
2006-11-16 22:43 ` Brian May
-- strict thread matches above, loose matches on Subject: below --
2006-11-16 16:01 Anh Vo
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox