comp.lang.ada
 help / color / mirror / Atom feed
* Subtypes with Combined Ranges
@ 2002-07-18 13:05 David Rasmussen
  2002-07-18 13:15 ` Lutz Donnerhacke
  2002-07-18 13:44 ` Jacob Sparre Andersen
  0 siblings, 2 replies; 4+ messages in thread
From: David Rasmussen @ 2002-07-18 13:05 UTC (permalink / raw)


An Ada-excercise reads as follows:

6.1 Write a program to count the number of occurrences of each letter of 
the alphabet typed as input at the keyboard. Using a subtype of 
Character as the index subtype of an array is a sensible way to do this.

My first thought was to do this:
subtype Alphabetical is Character range 'A'..'Z' | 'a'..'z';

But I get an error. What is the best type for this excercise?

/David




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Subtypes with Combined Ranges
  2002-07-18 13:05 Subtypes with Combined Ranges David Rasmussen
@ 2002-07-18 13:15 ` Lutz Donnerhacke
  2002-07-18 13:44 ` Jacob Sparre Andersen
  1 sibling, 0 replies; 4+ messages in thread
From: Lutz Donnerhacke @ 2002-07-18 13:15 UTC (permalink / raw)


* David Rasmussen wrote:
>6.1 Write a program to count the number of occurrences of each letter of
>    the alphabet typed as input at the keyboard. Using a subtype of
>    Character as the index subtype of an array is a sensible way to do
>    this.

>My first thought was to do this:
>subtype Alphabetical is Character range 'A'..'Z' | 'a'..'z';

A subtype has to be holefree.

>But I get an error. What is the best type for this excercise?

There are multiple solutions:
  - subtype Uppercase is Character range 'A' .. 'Z';
    subtype Lowercase is Character range 'a' .. 'z';
  - lettertest : array (Character) of Boolean :=
      ('A' .. 'Z' | 'a' .. 'z' => True, others => False);
  - lettermap : Ada.Strings.Maps.Character_Set :=
      To_Set (((Low => 'a', High => 'z'), (Low => 'A', High => 'Z')));
  - and many more.

Because you post your exercise here, you will get an other one:
 Implement at least ten different algorithms which print the elementary
 multiplication table. Post them here in two weeks.
 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Subtypes with Combined Ranges
  2002-07-18 13:05 Subtypes with Combined Ranges David Rasmussen
  2002-07-18 13:15 ` Lutz Donnerhacke
@ 2002-07-18 13:44 ` Jacob Sparre Andersen
  2002-07-18 14:48   ` David Rasmussen
  1 sibling, 1 reply; 4+ messages in thread
From: Jacob Sparre Andersen @ 2002-07-18 13:44 UTC (permalink / raw)


David Rasmussen wrote:

> An Ada-excercise reads as follows:
> 
> 6.1 Write a program to count the number of occurrences of each letter of 
> the alphabet typed as input at the keyboard. Using a subtype of 
> Character as the index subtype of an array is a sensible way to do this.
> 
> My first thought was to do this:
> subtype Alphabetical is Character range 'A'..'Z' | 'a'..'z';

I think your problem is that you misunderstand the exercise. 
"The alphabet" is just 'a' to 'z' and case insensitive. But 
since the alphabet actually is 'a' to '�', and not just 'a' 
to 'z', the advise in the exercise is inapropriate for other 
languages than English and Latin.

I would assume that the alphabet considered by the user is a 
subset of the type Character, but include all elements of 
type Character (converted to lower case) in the histogram, 
and not worry about which elements to show until printing 
out the histogram.

Jacob
-- 
"Any, sufficiently complicated, experiment is 
indistinguishable from magic."




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Subtypes with Combined Ranges
  2002-07-18 13:44 ` Jacob Sparre Andersen
@ 2002-07-18 14:48   ` David Rasmussen
  0 siblings, 0 replies; 4+ messages in thread
From: David Rasmussen @ 2002-07-18 14:48 UTC (permalink / raw)


Jacob Sparre Andersen wrote:
> David Rasmussen wrote:
> 
>> An Ada-excercise reads as follows:
>>
>> 6.1 Write a program to count the number of occurrences of each letter 
>> of the alphabet typed as input at the keyboard. Using a subtype of 
>> Character as the index subtype of an array is a sensible way to do this.
>>
>> My first thought was to do this:
>> subtype Alphabetical is Character range 'A'..'Z' | 'a'..'z';
> 
> 
> I think your problem is that you misunderstand the exercise. "The 
> alphabet" is just 'a' to 'z' and case insensitive. But since the 
> alphabet actually is 'a' to '�', and not just 'a' to 'z', the advise in 
> the exercise is inapropriate for other languages than English and Latin.
> 
> I would assume that the alphabet considered by the user is a subset of 
> the type Character, but include all elements of type Character 
> (converted to lower case) in the histogram, and not worry about which 
> elements to show until printing out the histogram.
> 
> Jacob

That is of course one way to do it. But I understood the word "subtype" 
in the excercise, in the Ada sense of the word. So I wanted to actually 
make a subtype, and then declare an array with this type as index type. 
I can do that with the lowercase letters, of course, as long as we're 
talking about English or Latin.

/David




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-07-18 14:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-18 13:05 Subtypes with Combined Ranges David Rasmussen
2002-07-18 13:15 ` Lutz Donnerhacke
2002-07-18 13:44 ` Jacob Sparre Andersen
2002-07-18 14:48   ` David Rasmussen

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