···········@yahoo.it (sacramento) writes:
Hello,
> Can you give me an example of a circular list in lisp please? How
> can I create it?
A circular list can only be created by mutation:
| (let ((*print-circle* t)
| (list (copy-list '(1 2 3))))
| (setf (cdr (last list)) list)
| (format t "; ~A~%" list))
=> ; List: #1=(1 2 3 . #1#)
This code modifies the CDR of the last cons of LIST to point back to
the beginning. It binds *PRINT-CIRCLE* to T to avoid an infinite loop
in the formatter.
You can also create circular lists as literals (mutation is still
necessary, but it is performed by the lisp reader) using the
"backreferencing" syntax shown above:
| (nth 1000 '#1=(1 2 3 . #1#))
=> 2
> thank you
Cu,
Dash.
--
Le pouvoir des médias supprime les raisonnements fondamentaux de la société
du spectacle.
-- Source : http://www.lillepop.org/fun/logomachie.html
--
http://users.swing.be/diederen/
On Mon, 31 May 2004 08:53:51 -0700, sacramento wrote:
> Can you give me an example of a circular list in lisp please? How can I create it?
USER(2): (setq *print-circle* t)
T
USER(3): (setq aa (list 1 2 3))
(1 2 3)
USER(4): (last aa)
(3)
USER(5): (setf (cdr (last aa)) aa)
#1=(1 2 3 . #1#)
--
···········@yahoo.it (sacramento) writes:
>
> Can you give me an example of a circular list in lisp please? How can I create it?
'#1=(a b c . #1#)
Be sure to either set *print-circle* to T or *print-length* to some
integer first, though.
--
Thomas A. Russ, USC/Information Sciences Institute