unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* About Circular Objects
@ 2012-12-25  8:52 Xue Fuqiao
  2012-12-25 14:52 ` Teemu Likonen
  0 siblings, 1 reply; 4+ messages in thread
From: Xue Fuqiao @ 2012-12-25  8:52 UTC (permalink / raw)
  To: help-gnu-emacs

I have a question about the #N# read syntax and #N= syntax.  Look at this code:

(progn
  (setq x '#1=(a #1#))
  (eq x (cdr x)))

Why does this code return nil?  Isn't the second element the list itself?

-- 
Best regards.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: About Circular Objects
       [not found] <mailman.16090.1356446630.855.help-gnu-emacs@gnu.org>
@ 2012-12-25 14:48 ` Lars Magne Ingebrigtsen
  2012-12-25 14:51 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 4+ messages in thread
From: Lars Magne Ingebrigtsen @ 2012-12-25 14:48 UTC (permalink / raw)
  To: Xue Fuqiao; +Cc: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> I have a question about the #N# read syntax and #N= syntax.  Look at
> this code:
>
> (progn
>   (setq x '#1=(a #1#))
>   (eq x (cdr x)))
>
> Why does this code return nil?  Isn't the second element the list itself?

(progn
  (setq x '#1=(a . #1#))
  (eq x (cdr x)))

=> t
  
-- 
(domestic pets only, the antidote for overdose, milk.)
  http://lars.ingebrigtsen.no  *  Lars Magne Ingebrigtsen



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: About Circular Objects
       [not found] <mailman.16090.1356446630.855.help-gnu-emacs@gnu.org>
  2012-12-25 14:48 ` About Circular Objects Lars Magne Ingebrigtsen
@ 2012-12-25 14:51 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 4+ messages in thread
From: Pascal J. Bourguignon @ 2012-12-25 14:51 UTC (permalink / raw)
  To: help-gnu-emacs

Xue Fuqiao <xfq.free@gmail.com> writes:

> I have a question about the #N# read syntax and #N= syntax.  Look at this code:
>
> (progn
>   (setq x '#1=(a #1#))
>   (eq x (cdr x)))
>
> Why does this code return nil?  Isn't the second element the list itself?

No.  Try it. (use M-x ielm). 


*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq x '#1=(a #1#))
#1=(a #1#)

ELISP> x
#1=(a #1#)

ELISP> (cdr x)
#1=((a . #1#))

ELISP> (eq x (second x))
t
ELISP> (eq x (cadr x))
t
ELISP> (eq x (cdr x))
nil
ELISP> 


If you want (eq x (cdr x)), then write:

    (setq x '#1=(a . #1#))

instead of:

    (setq x '#1=(a #1#))


ELISP> (setq x '#1=(a . #1#))
#1=(a . #1#)

ELISP> (eq x (cdr x))
t
ELISP> 


Not all circular structures are circular lists.  Even circular lists may
have a stem:


ELISP> (let ((circle '#1=(a . #1#)))
         (setq x (list* 'a 'a 'a circle)))
(a a a . #1=(a . #1#))

ELISP> x
(a a a . #1=(a . #1#))

ELISP> (eq (car x) (cadr x))
t
ELISP> (eq (cadr x) (caddr x))
t
ELISP> (eq (caddr x) (cadddr x))
t

and so on ad libitum, but:

ELISP> (eq x (cdr x))
nil
ELISP> 




-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: About Circular Objects
  2012-12-25  8:52 Xue Fuqiao
@ 2012-12-25 14:52 ` Teemu Likonen
  0 siblings, 0 replies; 4+ messages in thread
From: Teemu Likonen @ 2012-12-25 14:52 UTC (permalink / raw)
  To: Xue Fuqiao; +Cc: help-gnu-emacs

Xue Fuqiao [2012-12-25 16:52:49 +0800] wrote:

> I have a question about the #N# read syntax and #N= syntax. Look at
> this code:
>
> (progn
>   (setq x '#1=(a #1#))
>   (eq x (cdr x)))
>
> Why does this code return nil? Isn't the second element the list
> itself?

No. It's the CAR of the second cons cell that points to the list itself.
If you want the CDR of the first (and only) cons cell to point back to
the cons cell, you would use this:

    (progn
      (setq x '#1=(a . #1#)) ; Note the dotted list.
      (eq x (cdr x)))

    => t



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-12-25 14:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.16090.1356446630.855.help-gnu-emacs@gnu.org>
2012-12-25 14:48 ` About Circular Objects Lars Magne Ingebrigtsen
2012-12-25 14:51 ` Pascal J. Bourguignon
2012-12-25  8:52 Xue Fuqiao
2012-12-25 14:52 ` Teemu Likonen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).