Computer Science Canada

Just curious... enums?

Author:  wtd [ Mon Sep 27, 2004 2:19 pm ]
Post subject:  Just curious... enums?

I'm curious, I presume Turing provides an "enum" capability, but can't seem to find any references to how to create one. Anyone have the answer to this conundrum?

Author:  Mazer [ Mon Sep 27, 2004 2:49 pm ]
Post subject: 

Heh. I guess it does. I never actually thought about it until you suggested using it in someone else's post. But here, from the reference:

Quote:

enum enumerated type

Syntax An enumeratedType is:
enum (id { , id } )



Description The values of an enumerated type are distinct and increasing. They can be thought of as the values 0, 1, 2 and so on, but arithmetic is not allowed with these values.

Example
type color : enum (red, green, blue)
var c : color := color . red
var d : color := succ (c) % d becomes green

Details Each value of an enumerated type is the name of the type followed by a dot followed by the element's name, for example, color.red. Enumerated values can be compared for equality and for ordering. The succ and pred functions can be used to find the value following or preceding a given enumerated value. The ord function can be used to find the enumeration position of a value, for example, ord (color.red) is 0.
Enumerated types cannot be combined with integers or with other enumerated types.


Details It is illegal to declare an "anonymous" enum. The only legal declaration for an enum is in a type declaration. For example, the following is now illegal:

var a : array enum (red, green, blue) of int
Given that there is no (easy) way of generating an enum value without it being a named type, this should not impact any but the most bizarre code.

Details The "put" and "get" statement semantics have been expanded to allow put's and get's of enum values. The values printed and input are the element names themselves, case sensitive. For example, for

type colors : enum (red, green, blue)
var c : colors := colors . red
put c % outputs "red" (without the quotes)

Author:  wtd [ Mon Sep 27, 2004 3:04 pm ]
Post subject: 

Great. Thanks.

Author:  Jonny Tight Lips [ Mon Sep 27, 2004 4:45 pm ]
Post subject: 

I have a question. What is the use of this command? I don't really understand its usefulness?

Author:  wtd [ Mon Sep 27, 2004 6:33 pm ]
Post subject: 

Consider if you have an array of events called "week" (with one event per week), and you want to be able to look them up in some meaningful way.

code:
var Events : array 0..6 of string
Events(0) := "It's Sunday... do something Sabbath-y."

put Events(0)


vs.

code:
type Days = enum (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
var events : array 0..6 of string
events(Sunday) := "It's Sunday... do something Sabbath-y."

put events(Sunday)


Which do you prefer to look at? Smile

Author:  Cervantes [ Mon Sep 27, 2004 6:51 pm ]
Post subject: 

In turing, the code would look like this:

code:

type Days : enum (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
var events : array 0 .. 6 of string

events (ord (Days.Sunday)) := "It's Sunday... do something Sabbath-y."

put events (ord (Days.Sunday))


the question, which would you prefer to look at, is becoming less one-sided since we have to put those ords in. Confused

Author:  wtd [ Mon Sep 27, 2004 8:37 pm ]
Post subject: 

Weird.


: