From: Paul Dietz
Subject: constant cons?
Date: 
Message-ID: <8k5b51$85g$1@schbbs.mot.com>
  I'd like to know if there's a good way to implement a 'ccons' macro
in lisp.  It would behave like cons, except that if the two argument
forms are constantp, the result would be constantp (with only one cons
cell created.)

So,

 (ccons x y) is the same as (cons x y), but

 (ccons 1 2) is equivalent to '(1 . 2), and

 (ccons +a+ +b+) would be the same as some +g+ where
 (defconstant +g+ (cons +a+ +b+)), if +a+ and +b+ are constants.

	Paul

From: Barry Margolin
Subject: Re: constant cons?
Date: 
Message-ID: <cYq95.81$EI1.1041@burlma1-snr2>
In article <············@schbbs.mot.com>,
Paul Dietz <·····@comm.mot.com> wrote:
>  I'd like to know if there's a good way to implement a 'ccons' macro
>in lisp.  It would behave like cons, except that if the two argument
>forms are constantp, the result would be constantp (with only one cons
>cell created.)
>
>So,
>
> (ccons x y) is the same as (cons x y), but
>
> (ccons 1 2) is equivalent to '(1 . 2), and
>
> (ccons +a+ +b+) would be the same as some +g+ where
> (defconstant +g+ (cons +a+ +b+)), if +a+ and +b+ are constants.

(defmacro ccons (car cdr &environment env)
  (if (and (constantp car env) (constantp cdr env))
      `(load-time-value (cons ,car ,cdr) t)
      `(cons ,car ,cdr)))

If you have two similar CCONS invocations, it's up to the compiler to
detect them and coalesce the constants.  LOAD-TIME-VALUE with read-only-p =
T tells the compiler that it's OK to do this, but doesn't force it to find
all the matches and coalesce them.  So it's unspecified whether

	(eq (ccons 1 2) (ccons 1 2))

will be true or false.  If you need something better, you'll have to
implement your own interning scheme; a hash table containing all the conses
that CCONS has created can be useful for this.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, 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: Paul Dietz
Subject: Re: constant cons?
Date: 
Message-ID: <8k5gr0$cab$1@schbbs.mot.com>
In article <·················@burlma1-snr2>,
Barry Margolin  <······@genuity.net> wrote:

>(defmacro ccons (car cdr &environment env)
>  (if (and (constantp car env) (constantp cdr env))
>      `(load-time-value (cons ,car ,cdr) t)
>      `(cons ,car ,cdr)))


Thanks.  This is very nearly what I wanted -- I was
missing the incantation 'load-time-value'.

Constantp doesn't necessarily recognize that
(load-time-value ... t)  can be considered constant,
but the macro can be written to do that itself.

	Paul