comp.lang.ada
 help / color / mirror / Atom feed
From: dww@math.fu-berlin.de (Debora Weber-Wulff)
Subject: Re: Membership in "set" of characters
Date: Mon, 22 Mar 1993 12:22:49 GMT
Date: 1993-03-22T12:22:49+00:00	[thread overview]
Message-ID: <N0TBB2RT@math.fu-berlin.de> (raw)
In-Reply-To: 1ok4ts$3cv@huon.itd.adelaide.edu.au


Thanks to all who wrote with information about my problems
with membership and FIXED/FLOAT. Here's a short summary of the
other answers I received:

FIXED/FLOAT

Just using 
  type DM is delta 0.01 range 0.0 .. 1000.0; 
gets me in trouble because the delta is a positive fraction
that causes rounding errors. I need
  for DM'small use 0.01;
to make it work as I expect, but I've been advised to leave FIXED
alone until I understand more about Ada :-) 

I've included a complete letter with some great advice at the end
of this summary.

There's an article in Ada Europe'92 proceedings [is that published
in LNCS? I've not heard of it before, but they seem to be in
their 13th year! Is there a FAQ for this group??? ] by Ben Brosgol,
Robert Eachus and Dave Emery about Ada Decimal Arithmetic.

ajpo.sei.cmu.edu (128.237.2.253) has a library of representations
for decimal arithmetic at ADAR-1.0.tar.Y and a book called
"Ada Quality and Style"

Membership Problem

Lots of suggestions!
* Use subtypes!
* use a case statement!
* make a function is_in (c:character; s:string) return Boolean is ...
* use okay := affirmatives'VALUE (CHARACTER'IMAGE(ch)) IN affirmatives;
* roll my own set as described below - that sounds like the
way I want to go. It reads a tad easier than the previous 
suggestion :-)

----- included file -----
In article <J6QBBP3L@math.fu-berlin.de> you write:
> This is another one of those "why won't this work?" for the experts.
> I'm stumped. As far as I can decode the LRM and
> see from 2 textbooks I consulted, this ought to work.

nope

> -- Why won't membership work here?

here's the short answer:

  for the type of thing you are doing, you can create a set ADT.
a simple way to do that in Ada for a relatively small discrete universe
like character is to use a boolean array.  you can pack it to make it
smaller, which effectively creates a bitmap. this is what Pascal sets
basically are - and this way Ada doesnt have to explicitly support
sets in the language. its just one kind of data type that you can build.

type char_predicates is array (character) of boolean;
pragma pack (char_predicates);
affirmative : constant char_predicates
 := char_predicates'('y'|'Y'|'j'|'J'|'o'|'O' => true, others => false);
...

okay := affirmative(ch);

you could also make affirmative a function instead of a constant array
to save space.  you could do this later if necessary without changing
the clients because the syntax for a function call is identical
to that of an array reference.

its a little more typing than Pascal in this case, but its
much easier to tell whats going on.

> I'm currently quite frustrated with the type system after writing a little program
> to calculate the return on investment.
> Is it me? Is it Meridian? Or is this the agony of using Ada?

It takes a while to get used to Ada's strong typing.  It seems overly
strict at first, but once you understand the rules well, they dont get
in the way very much at all.  in fact, they often help prevent errors.
still that initial period can be rough, since its easy to make things
much stricter than you need without realizing it.  and then blame the
language when it is enforcing exactly the rules that you asked for.
derived types are a frequent culprit.  they can be very helpful when
you need them, but can easily make it difficult to write any
expression if over-used.

you can get very similar behavior to other languages like Pascal with
respect to numeric types if you wish.

1.  avoid derived types until you have learned the rest of Ada well.
  then use them judiciously and carefully.
  (derived types have the form "type xxx is new yyy")
  (also avoid the form "type xxx is range a .. b" at first)

2. use subtypes instead, "subtype xxx is yyy range a .. b"

3. avoid fixed point, use float instead.

4. if you frequently use a mixed mode operation (i.e. multiply integers
and floats to get floats) then define a function so that you dont
have to keep performing conversions.

  function "*" (left : integer; right : float) return float is ...

5. when you feel comfortable with Ada, get a few books and try out
derived types and floats. they do have good uses.  the book
Ada Quality and Style available on ajpo.sei.cmu.edu has a section
on derived types and subtypes that might help (disclaimer:
I helped write that part)  the FAQ on the same machine describes
several good books as well (thanks to Mike Feldman).

good luck. 

P.S. Meridian is a pretty good compiler, but if you get the chance
or have something really serious, use DECs.
-- 
---------------------------------------------------
Alex Blakemore alex@cs.umd.edu   NeXT mail accepted

----- end included file -----

-- 
Debora Weber-Wulff, Professorin fuer Softwaretechnik
snail: Technische Fachhochschule, FB Informatik, 
       Luxemburgerstr. 10, 1000 Berlin 65
email: dww@informatik.tfh-berlin.dbp.de 



  reply	other threads:[~1993-03-22 12:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-03-19 17:06 Membership in "set" of characters Debora Weber-Wulff
1993-03-19 19:57 ` Mark A Biggar
1993-03-22 10:37   ` Andrew Dunstan,,2285592,
1993-03-22 12:22     ` Debora Weber-Wulff [this message]
1993-03-22 17:11       ` Robert I. Eachus
1993-03-22 15:49   ` Ada 9X references M. Ramchandran
1993-03-22 21:50     ` Alex Blakemore
1993-03-23  4:16     ` Michael Feldman
replies disabled

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