Generate lower case ASCII alphabet: Difference between revisions
Content deleted Content added
First draft of the task |
+ Ada and D entries |
||
Line 3: | Line 3: | ||
If the standard library contains such sequence, show how to access it, but don't fail to show how to generate a similar sequence. For this basic task use a reliable style of coding, a style fit for a large program, and use strong typing if available. |
If the standard library contains such sequence, show how to access it, but don't fail to show how to generate a similar sequence. For this basic task use a reliable style of coding, a style fit for a large program, and use strong typing if available. |
||
=={{header|Ada}}== |
|||
<lang ada>procedure Alphabet is |
|||
type Arr_Type is array (Integer range <>) of Character; |
|||
Arr : Arr_Type (1 .. 26); |
|||
C : Character := 'a'; |
|||
begin |
|||
for I in Arr'Range loop |
|||
Arr (I) := C; |
|||
C := Character'Succ (C); |
|||
end loop; |
|||
end;</lang> |
|||
=={{header|D}}== |
|||
The lower case ASCII letters of the Phobos standard library: |
|||
<lang d>import std.ascii: lowercase; |
|||
void main() {}</lang> |
|||
The generation of the ASCII alphabet array: |
|||
<lang d>void main() { |
|||
char[26] arr; |
|||
foreach (immutable i, ref c; arr) |
|||
// c = 'a' + i; |
|||
c = cast(char)('a' + i); |
|||
}</lang> |
|||
An alternative version: |
|||
<lang d>import std.range, std.algorithm, std.array; |
|||
void main() { |
|||
char[26] arr = 26 |
|||
.iota |
|||
.map!(i => cast(char)('a' + i)) |
|||
.array; |
|||
}</lang> |
Revision as of 15:07, 2 August 2013
Generate lower case ASCII alphabet is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Generate an array, list, lazy sequence, or even an indexable string of all the lower case ASCII characters, from 'a' to 'z'.
If the standard library contains such sequence, show how to access it, but don't fail to show how to generate a similar sequence. For this basic task use a reliable style of coding, a style fit for a large program, and use strong typing if available.
Ada
<lang ada>procedure Alphabet is
type Arr_Type is array (Integer range <>) of Character; Arr : Arr_Type (1 .. 26); C : Character := 'a';
begin
for I in Arr'Range loop Arr (I) := C; C := Character'Succ (C); end loop;
end;</lang>
D
The lower case ASCII letters of the Phobos standard library: <lang d>import std.ascii: lowercase;
void main() {}</lang>
The generation of the ASCII alphabet array: <lang d>void main() {
char[26] arr;
foreach (immutable i, ref c; arr) // c = 'a' + i; c = cast(char)('a' + i);
}</lang>
An alternative version: <lang d>import std.range, std.algorithm, std.array;
void main() {
char[26] arr = 26 .iota .map!(i => cast(char)('a' + i)) .array;
}</lang>