Enumerations: Difference between revisions
No edit summary |
→[[Toka]]: Updated to reflect the addition of enum| function in Toka 1.1 |
||
Line 225:
[[Category:Toka]]
Toka has no data types, and therefore no actual enumeration type.
optional library function which does provide a way to create enumerated
values easily though.
This library function takes a starting value and a list of names as shown in the
example below.
10 enum| foo bar baz |
▲ ] is enum|
▲ 0 enum| apple banana cherry |
==[[Visual Basic .NET]]==
|
Revision as of 03:20, 14 September 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Create an enumeration of types with and without values.
Ada
Ada enumeration types have three distinct attributes, the enumeration literal, the enumeration position, and the representation value. The position value is implied from the order of specification of the enumeration literals in the type declaration. The position values provide the ordering for the enumeration values. In the example below apple is less than banana which is less than cherry due to their positions, not due to their enumeration literal. There is no necessary relationship between the enumeration position and the enumeration representation.
type Fruit is (apple, banana, cherry); - No specification of the representation value; for Fruit use (apple => 1, banana => 2, cherry => 3); - specification of the representation values
Ada enumeration types are non-numeric discrete types. They can be used to index arrays, but there are no arithmetic operators for enumeration types. Characters are implemented as an enumeration type in Ada.
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
REM Impossible. Can only be faked with arrays of strings. OPTION BASE 1 DIM SHARED fruitsName$(1 to 3) DIM SHARED fruitsVal%( 1 to 3) fruitsName$[1] = "apple" fruitsName$[2] = "banana" fruitsName$[3] = "cherry" fruitsVal%[1] = 1 fruitsVal%[2] = 2 fruitsVal%[3] = 3
REM OR GLOBAL CONSTANTS DIM SHARED apple%, banana%, cherry% apple% = 1 banana% = 2 cherry% = 3
C
Compiler: GCC, MSVC, BCC, Watcom
Libraries: Standard
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };
C++
Compiler: GCC, Visual C++, BCC, Watcom
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };
C#
enum fruits { apple, banana, cherry }
enum fruits { apple = 0, banana = 1, cherry = 2 }
enum fruits : int { apple = 0, banana = 1, cherry = 2 }
E
Simple group of object definitions (value methods could be left out if appropriate):
def apple { to value() { return 0 } } def banana { to value() { return 1 } } def cherry { to value() { return 2 } }
With a guard for type checks:
interface Fruit guards FruitStamp {} def apple implements FruitStamp {} def banana implements FruitStamp {} def cherry implements FruitStamp {} def eat(fruit :Fruit) { ... }
With and without values, using a hypothetical enumeration library:
def [Fruit, [=> apple, => banana, => cherry]] := makeEnumeration()
def [Fruit, [=> apple, => banana, => cherry]] := makeEnumeration(0, ["apple", "banana", "cherry"])
Forth
Forth has no types, and therefore no enumeration type. To define sequential constants, a programmer might write code like this:
0 CONSTANT apple 1 CONSTANT banana 2 CONSTANT cherry ...
However, a common idiom in forth is to define a defining word, such as:
: ENUM ( n -<name>- n+1 ) DUP CONSTANT 1+ ;
This word defines a new constant of the value specified and returns the next value in sequence. It would be used like this:
0 ENUM APPLE ENUM BANANA ENUM CHERRY DROP
Or you can use CONSTANT to capture the "end" value instead of dropping it:
0 ENUM FIRST ENUM SECOND ... CONSTANT LAST
A variation of this idea is the "stepped enumeration" that increases the value by more than 1, such as:
: SIZED-ENUM ( n s -<name>- n+s ) OVER CONSTANT + ; : CELL-ENUM ( n -<name>- n+cell ) CELL SIZED-ENUM ;
A programmer could combine these enum definers in any way desired:
0 ENUM FIRST \ value = 0 CELL-ENUM SECOND \ value = 1 ENUM THIRD \ value = 5 3 SIZED-ENUM FOURTH \ value = 6 ENUM FIFTH \ value = 9 CONSTANT SIXTH \ value = 10
Note that a similar technique is often used to implement structures in Forth.
For a simple zero-based sequence of constants, one could use a loop in the defining word:
: CONSTANTS ( n -- ) 0 DO I CONSTANT LOOP ;
\ resistor digit colors 10 CONSTANTS black brown red orange yellow green blue violet gray white
Haskell
data Fruit = Apple | Banana | Cherry deriving Enum
Java
Java 1.5 only
enum fruits { apple, banana, cherry }
enum fruits { apple(0), banana(1), cherry(2) private final int value; fruits(int value) { this.value = value; } public int value() { return value; } }
JavaScript
var fruits { apple, banana, cherry }; var fruits { apple : 0, banana : 1, cherry : 2 };
JSON
fruits { apple, banana, cherry }; fruits { apple : 0, banana : 1, cherry : 2 };
JScript.NET
enum fruits { apple, banana, cherry } enum fruits { apple = 0, banana = 1, cherry = 2 }
Lisp
(setf FRUIT '(apple banana cherry))
as lisp is a symbolic language assigning values to the symbols is unnecessary.
Perl
Interpeter: Perl
# Using an array my @fruits = qw(apple banana cherry);
# Using a hash my %fruits = ( apple => 0, banana => 1, cherry => 2 );
PHP
// Using an array/hash $fruits = array( "apple", "banana", "cherry" ); $fruits = array( "apple" => 0, "banana" => 1, "cherry" => 2 );
Python
Interpreter: Python 2.5
There is no special syntax, typically global variables are used with range:
FIRST_NAME, LAST_NAME, PHONE = range(3)
Seed7
const type: fruits is new enum apple, banana, cherry end enum;
Tcl
The following elegant example comes straight from the [Tcl wiki:]
proc enumerate {name values} { interp alias {} $name: {} lsearch $values interp alias {} $name@ {} lindex $values }
it would be used like this:
enumerate fruit {apple blueberry cherry date elderberry} fruit: date ==> prints "3" fruit@ 2 ==> prints "cherry"
Toka
Toka has no data types, and therefore no actual enumeration type. There is an optional library function which does provide a way to create enumerated values easily though.
This library function takes a starting value and a list of names as shown in the example below.
needs enum 0 enum| apple banana carrot | 10 enum| foo bar baz |
Visual Basic .NET
' Is this valid?! Enum fruits apple banana cherry End Enum
' This is correct Enum fruits apple = 0 banana = 1 cherry = 2 End Enum