From: Grunion
Subject: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <qip7uukqnqgp064qnlj475b7s63p9kecfk@4ax.com>
I have been doing research on several different programming languages
and I wanted to know more about the records for Lisp. I know Common
Lisp doesn't directly support records, but they can be model by using
structures. My question is this:

I want to model a "variant record" with Common Lisp, and wish to know
how to do this. I know many languages have variant records, and I am
intrigued to know how this is done in Common Lisp. Is this possible?
How do you pass a parameter to this ... etc.  If you haven't guessed
by now, I don't know much about Common Lisp. :)

Cheers!
Grunion

From: Barry Margolin
Subject: Re: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <0PRE9.42$fP2.2007@paloalto-snr1.gtei.net>
In article <··································@4ax.com>,
Grunion  <·······@misplacedGrunion.com> wrote:
>I have been doing research on several different programming languages
>and I wanted to know more about the records for Lisp. I know Common
>Lisp doesn't directly support records, but they can be model by using
>structures. My question is this:

IMHO, "structure" and "record" are the names that different programming
languages use for the same things: heterogeneous tuples with named members.

>I want to model a "variant record" with Common Lisp, and wish to know
>how to do this. I know many languages have variant records, and I am
>intrigued to know how this is done in Common Lisp. Is this possible?
>How do you pass a parameter to this ... etc.  If you haven't guessed
>by now, I don't know much about Common Lisp. :)

Common Lisp uses manifest types, so you don't usually need variant records.
Any type of object can be passed to a function, and the function can
dispatch on the type if it wishes, e.g.

(if (typep thing 'my-structure-type)
    ...)

or:

(typecase thing
  (type1 (do-something thing))
  (type2 (do-something-else thing))
  ...)

Common Lisp also supports object-oriented programming, so you can define
generic functions and methods on them, and the system will dispatch
automatically.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Kent M Pitman
Subject: Re: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <sfwwun0ovae.fsf@shell01.TheWorld.com>
Grunion <·······@misplacedGrunion.com> writes:

> I have been doing research on several different programming languages
> and I wanted to know more about the records for Lisp. I know Common
> Lisp doesn't directly support records, but they can be model by using
> structures. My question is this:
> 
> I want to model a "variant record" with Common Lisp, and wish to know
> how to do this. I know many languages have variant records, and I am
> intrigued to know how this is done in Common Lisp. Is this possible?
> How do you pass a parameter to this ... etc.  If you haven't guessed
> by now, I don't know much about Common Lisp. :)

Instead of making variant types, we would define a base class that would
contain the common elements and then make each of the variant types its
own class.   For example, if you have class PERSON which has a mother/father
slot if the person was born normally or just a parent slot if the person was
cloned, based on a boolean cloned value in the record, you would do

 (defclass person ()
   ((name    :initarg :name    :accessor name    :type string)
    (cloned? :initarg :cloned? :accessor cloned? :type boolean)))

 (defclass classic-person (person)
   ((mother  :initarg :mother  :accessor mother  :type (or person null))
    (father  :initarg :father  :accessor father  :type (or person null))))

 (defclass cloned-person (person)
   ((parent  :initarg :parent  :accessor parent  :type (or person null))))
 
Does that help?
From: Pascal Costanza
Subject: Re: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <as0r44$2l6$1@newsreader2.netcologne.de>
Grunion wrote:
> I have been doing research on several different programming languages
> and I wanted to know more about the records for Lisp. I know Common
> Lisp doesn't directly support records, but they can be model by using
> structures. My question is this:
> 
> I want to model a "variant record" with Common Lisp, and wish to know
> how to do this. I know many languages have variant records, and I am
> intrigued to know how this is done in Common Lisp. Is this possible?
> How do you pass a parameter to this ... etc.  If you haven't guessed
> by now, I don't know much about Common Lisp. :)

The best sources for this kind of information are the HyperSpec at 
http://www.lispworks.com/reference/HyperSpec/ (chapter 8) and "Common 
Lisp The Language, 2nd edition" at 
http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/cltl2.html 
(chapter 2). If you don't like specifications you should look at 
http://www.lisp.org that offers links to tutorials, for example.

I don't know if one can have something similar to variant records with 
structures in Common Lisp, but generally you shouldn't do that. As soon 
as you can have inheritance between record types you don't need variant 
records anymore. One of the best articles (IMHO) on this topic is "Type 
Extensions" by Niklaus Wirth (http://doi.acm.org/10.1145/42190.46167).

In Common Lisp, structures can extend each other. Furthermore, the 
concept of type extensions (or better: inheritance) is a basic 
ingredient of most object-oriented languages and is also available in 
CLOS (the Common Lisp Object System that is part of Common Lisp).

I hope this helps.

Pascal

P.S.: As a sidenote, the term "type extensions" has never become part of 
mainstream terminology.

-- 
Given any rule, however �fundamental� or �necessary� for science, there 
are always circumstances when it is advisable not only to ignore the 
rule, but to adopt its opposite. - Paul Feyerabend
From: grunion
Subject: Re: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <3kv7uucnaqr4j15psk9jjabmgm5iqmce5k@4ax.com>
On Tue, 26 Nov 2002 23:03:52 +0100, Pascal Costanza <········@web.de>
wrote:

Thankyou everyone for your answers. :)  Your useful knowledge has
indeed enlightened me, and pointed me in the right direction.

Many thanks!
grunion
From: Coby Beck
Subject: Re: Hello - I have a question about records, please read inside!!
Date: 
Message-ID: <as0q69$3kj$1@otis.netspace.net.au>
"Grunion" <·······@misplacedGrunion.com> wrote in message
·······································@4ax.com...
> I have been doing research on several different programming languages
> and I wanted to know more about the records for Lisp. I know Common
> Lisp doesn't directly support records, but they can be model by using
> structures. My question is this:
>
> I want to model a "variant record" with Common Lisp, and wish to know
> how to do this. I know many languages have variant records, and I am
> intrigued to know how this is done in Common Lisp. Is this possible?
> How do you pass a parameter to this ... etc.  If you haven't guessed
> by now, I don't know much about Common Lisp. :)
>

How is your idea of a "record" different from a CL structure?

I think you will find that concepts like C's union and what I guess you mean
by a "variant record" are nothing special in Lisp because variables do not
need to be typed.

(setf person (make-employee))
(setf person (make-customer))
(setf person 'fred)

person can be whatever you need it to be.  You can dispatch methods on
person based on the type of the value it is currently bound to.  You can
also find out its type via (type-of person)

CL-USER 1 > (defstruct employee
              name salary)
EMPLOYEE
CL-USER 2 > (defstruct customer
              name account-num)
CUSTOMER
CL-USER 3 > (defvar person (make-employee))
PERSON
CL-USER 4 > (type-of person)
EMPLOYEE
CL-USER 5 > (setf person (make-customer))
#S(CUSTOMER NAME NIL ACCOUNT-NUM NIL)
CL-USER 6 > (type-of person)
CUSTOMER
CL-USER 7 > (setf person 'fred)
FRED
CL-USER 8 > (type-of person)
SYMBOL

Check www.lisp.org for online resources including tutorials.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")