From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,edafb2ab7e8839bc X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!tiscali!newsfeed1.ip.tiscali.net!proxad.net!cleanfeed2-a.proxad.net!nnrp18-1.free.fr!not-for-mail Message-ID: <455cf4f8$0$17901$426a74cc@news.free.fr> From: Yves Bailly Subject: Re: Char type verification Newsgroups: comp.lang.ada Date: Fri, 17 Nov 2006 00:30:51 +0100 References: <1163628033.606530.190550@i42g2000cwa.googlegroups.com> <1163627827.1632.10.camel@localhost.localdomain> <1163632532.101867.123200@m73g2000cwd.googlegroups.com> User-Agent: KNode/0.10.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit Organization: Guest of ProXad - France NNTP-Posting-Date: 17 Nov 2006 00:32:09 MET NNTP-Posting-Host: 81.56.171.53 X-Trace: 1163719929 news-3.free.fr 17901 81.56.171.53:51821 X-Complaints-To: abuse@proxad.net Xref: g2news2.google.com comp.lang.ada:7515 Date: 2006-11-17T00:32:09+01:00 List-Id: 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 | //\ \_/ | | \_/`