Re: Two questions about eq
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Mon, 31 Mar 2008 21:57:08 +0200
Scott Burson <FSet.SLB@xxxxxxxxx> writes:
On Mar 31, 8:49 am, Spiros Bousbouras <spi...@xxxxxxxxx> wrote:
1) The HS says that
(eq (cons 'a 'b) (cons 'a 'b))
always returns false but
(eq '(a . b) '(a . b))
might be true. Why is this ?
Because the compiler is allowed to select a canonical instance for
quoted constants (since you're not supposed to modify them). So, the
compiler could use the same cons for both occurrences of the quoted
dotted pair.
Explicitly, you can implement ' reader macro as:
(set-macro-character #\'
(let ((literal-stuff (make-hash-table :test (function equal))))
(lambda (stream quote)
(declare (ignore quote))
(let ((new-literal (read stream nil t)))
(or (gethash new-literal literal-stuff)
(setf (gethash new-literal literal-stuff) new-literal)))))
nil *readtable*)
2) Under what (realistic) circumstances might
(eq 1 1) be false ?
For example, imagine you're implementing CL over perl. I mean,
perhaps you have to write perl script, but you just cannot stand perl
syntax, so you decide to implement a CL over perl to be able to
produce perl scripts... Ok then, in perl data is not typed. 1 ==
"1". When you have 1 in perl you cannot tell if it's a string, an
integer, a float or what. So you implement all lisp objects as a perl
hash, with a type slot and a value slot. When you write (eq 1 1),
it's translated to:
lisp_eq({type=>integer,value=>1},{type=>integer,value=>1});
and of course, the two hash tables are not 'eq'.
Otherwise, very few are the cl implementations that have not (eq 1 1),
but most of them have (not (eq (1+ most-positive-fixnum)
(1+ most-positive-fixnum))).
Realistically, none, but use `eql' anyway.
-- Scott
--
__Pascal Bourguignon__ http://www.informatimago.com/
"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
.
- References:
- Two questions about eq
- From: Spiros Bousbouras
- Re: Two questions about eq
- From: Scott Burson
- Two questions about eq
- Prev by Date: (parse-decimal-fraction <string>) --> <rational> ?
- Next by Date: Re: How big a programme to properly appreciate CL ?
- Previous by thread: Re: Two questions about eq
- Next by thread: Re: Two questions about eq
- Index(es):
Relevant Pages
|