Re: Enumerations
- From: Dan Bensen <randomgeek@xxxxxxxxxxxxxx>
- Date: Sat, 10 Nov 2007 14:25:19 -0600
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/
.
- References:
- Enumerations
- From: Don Geddis
- Re: Enumerations
- From: Barry Margolin
- Re: Enumerations
- From: Don Geddis
- Enumerations
- Prev by Date: Re: Starting up Slime
- Next by Date: Re: mod_lisp over CGI
- Previous by thread: Re: Enumerations
- Next by thread: Re: Enumerations
- Index(es):
Relevant Pages
|