Re: Enumerations



Don Geddis wrote:
> So, way back when I was first learning how to program, I think I used Pascal
> (UCSD's p-system, IIRC). It had this nice concept of an "enumeration".
> I'm not going to remember the syntax, but it worked something like this:
> enumeration Fruit: (Apple, Banana, Carrot);
> array Weight [ Fruit ];
> Weight[Apple] := 3;
> Weight[Banana] := 5;
> Weight[Carrot] := 2;

C++ has this too.

enum fruit { APPLE, BANANA, CARROT, NUM_FRUITS };
int Weights[NUM_FRUITS];
....

> Common Lisp doesn't seem to have enumerations like this built in (unless I
> missed something). You guys ever program in this style? What technique do
> you use to get the same effect as I recall from Pascal?

> Barry Margolin <barmar@xxxxxxxxxxxx> wrote on Fri, 09 Nov 2007:
>> Alternatively, you can use a more object-oriented approach with
>> structures or classes:

This is what Martin Fowler recommended in his book Refactoring.
But add a name (and carrots aren't a fruit :)

(defstruct fruit () name weight really-a-fruit
...)
(defvar *apple* (make-fruit :name "apple" :weight 3 :really-a-fruit t ...))
(defvar *banana* (make-fruit :name "banana" :weight 5 :really-a-fruit t ...))
(defvar *carrot* (make-fruit :name "carrot" :weight 2 :really-a-fruit nil ...))

> OK, but you got rid of my array.

> (setq *fruits* (vector *apple* *banana* *carrot*))
> Now there's an array.

--
Dan
www.prairienet.org/~dsb/
.



Relevant Pages

  • Re: How to calculate: sumproduct / db??
    ... returns an array of TRUE/FALSE values. ... single unary, -, transforms the TRUE/FALSE to 1/0, but a negative 1. ... number of pieces fruit in a box ...
    (microsoft.public.excel.programming)
  • Re: Multiple ComboBoxes, one data source
    ... have to make sure that each combobox uses a separate BindingContext. ... You could also get around this by copying the contents of the array into ... Fruit 2: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Amanita Muscaria; Herb of Immortality - New Book and Group
    ... >>> Mushrooms can't be herbs, they are fungi. ... >>if the European Market can classify a carrot as a fruit, ... Some sort of tax fiddle IIRC, and I don't care a carrot isn't a fruit, a ...
    (uk.religion.pagan)
  • Re: INDEX function array causes problem when attempting to use LARGE function to sort positive and n
    ... In your second Sheet in B2 enter ... function with positive and negative numbers "WITHOUT" using an array ... Orange Fruit -200 ... TRUE values in the INDEX array that are negative or zero ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Experience and style.
    ... I have never met a fruit that tasted better after being ... In the EU tomatoes are *occasionally* classed as fruit, ... carrots - but only very occasionally because of the "jam" made ... Carrot jam? ...
    (rec.arts.sf.fandom)