From: Rahul Jain
Subject: Enumerated types?
Date: 
Message-ID: <38301B39.18CF2E43@owlnet.rice.edu>
What's the best way to define an enumerated type in Lisp? I've looked
around and can't find any clear explanation of how to do this. My
intuition says to define some symbols and then create a deftype that
specifies that an object of that type must be one of those symbols.
How would I code this?

Also, I can't find a class for an alist. Is there any way to specify
that the type of a variable must be an alist (even better, have the
types of the keys and values specifyable)? I can always use cons as a
fallback, but I'd rather specify exactly what I want since I'll have
some not-so-experienced programmers working on this project with me :)

--
-> -=-=-=-=-=-=-=-=-=- <  Rahul -=- Jain  > -=-=-=-=-=-=-=-=-=- <-
-> "I never could get the hang of Thursdays." -Douglas N. Adams <-
-> -=-=-=-  URL: http://hoohoo.brrsd.k12.nj.us/~rjain/  -=-=-=- <-
-> -=-=-=-=-=-  E-mail:  ·················@usa.net  -=-=-=-=-=- <-
    Version 9.105.999.1111111111111.23.042
    (c)1996-1998, All rights reserved.
    Disclaimer available upon request.

From: Frode Vatvedt Fjeld
Subject: Re: Enumerated types?
Date: 
Message-ID: <2hyabzepjd.fsf@dslab7.cs.uit.no>
Rahul Jain <·····@owlnet.rice.edu> writes:

> What's the best way to define an enumerated type in Lisp?

I think that depends on which directions of association between
numbers and symbols you will need. If you just need to map symbols to
numbers, you can obviously just use the symbols' value-cell or a
property slot. If you (also) need to map the numbers to symbols, I
think an eql hash-table will do the job.

> I've looked around and can't find any clear explanation of how to do
> this. My intuition says to define some symbols and then create a
> deftype that specifies that an object of that type must be one of
> those symbols.  How would I code this?

Hum. If you just need a set of identifiers, then that is not an
enumerated type. Although one uses enumerated types to implement sets
of identifiers in C, for example. I believe a type specifier for a set
of identifiers is expressed in lisp as (MEMBER symbol1 symbol2 ...).
Here, the identity of the symbols (their addresses) play the same role
as the numbers assigned (implicitly or not) to the labels in C enums,
but that's all behind the scenes in lisp.

-- 
Frode Vatvedt Fjeld
From: Marco Antoniotti
Subject: Re: Enumerated types?
Date: 
Message-ID: <lwiu32vm4a.fsf@parades.rm.cnr.it>
Frode Vatvedt Fjeld <······@acm.org> writes:

> Rahul Jain <·····@owlnet.rice.edu> writes:
> 
> > What's the best way to define an enumerated type in Lisp?

(deftype colors () '(member :red :green :blue))

> I think that depends on which directions of association between
> numbers and symbols you will need.

The fat that in C you can do

	enum { red = 22, green = 124, blue = 3 }

Does not mean it is an idea that must be carried over to other
laguages.  AFAIK, Java does not have enumerated types and Ada uses a
rather different scheme for enumerations.

> If you just need to map symbols to
> numbers, you can obviously just use the symbols' value-cell or a
> property slot. If you (also) need to map the numbers to symbols, I
> think an eql hash-table will do the job.
> 
> > I've looked around and can't find any clear explanation of how to do
> > this. My intuition says to define some symbols and then create a
> > deftype that specifies that an object of that type must be one of
> > those symbols.  How would I code this?
> 
> Hum. If you just need a set of identifiers, then that is not an
> enumerated type. Although one uses enumerated types to implement sets
> of identifiers in C, for example. I believe a type specifier for a set
> of identifiers is expressed in lisp as (MEMBER symbol1 symbol2 ...).
> Here, the identity of the symbols (their addresses) play the same role
> as the numbers assigned (implicitly or not) to the labels in C enums,
> but that's all behind the scenes in lisp.

That is correct.  BTW.  In the AI.Repository there is some code that
does that explicitely.  If I remember correctly, it is by Norvig.

Cheers

-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.parades.rm.cnr.it/~marcoxa
From: ···········@my-deja.com
Subject: Re: Enumerated types?
Date: 
Message-ID: <80pime$d5l$1@nnrp1.deja.com>
In article <·················@owlnet.rice.edu>,
  Rahul Jain <·····@owlnet.rice.edu> wrote:
> What's the best way to define an enumerated type in Lisp?

(deftype smurf () '(member handy brainy brawny papa smurfette))


Sent via Deja.com http://www.deja.com/
Before you buy.
From: Axel Schairer
Subject: Re: Enumerated types?
Date: 
Message-ID: <fm9k8nj7hz7.fsf@skiach.dai.ed.ac.uk>
Rahul Jain <·····@owlnet.rice.edu> writes:
> What's the best way to define an enumerated type in Lisp? 

Rahul,

I don't know whether this is what you want, but you may be looking for
something like

   (defstruct color)
   (defstruct (red (:include color)))
   (defstruct (blue (:include color)))
   ...

   (make-red) => #S(RED )
   (make-blue) => #S(BLUE )
   (color-p (make-red)) => T
   (etypecase (make-red)
     (red 1)
     (blue 2)) => 1

or alternatively

   (defstruct color name)
   (defun make-red () (make-color :name :red))
   (defun make-blue () (make-color :name :blue))

   (make-blue) => #S(COLOR :NAME :BLUE)
   (color-p (make-red)) => T
   (ecase (color-name (make-red))
     (:red 1)
     (:blue 2)) => 1

The problem with both is that (EQL (MAKE-RED) (MAKE-RED)) is false.
But you could easily define one constant for red and always return its
value as the result of MAKE-RED (or document a constant +RED+ instead
of the function MAKE-RED) if this matters to you.

(The first may be a lot more expensive in terms of compilation time
and executable size if you consider a huge amount of colors.  I know
at least one implementation that records a huge amount of useful
information when compiling structure definitions and stores the
information in the compiled files.  But then this may not be a problem
at all.)

Hope this helps.

Cheers,
Axel