Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Q]: Need help with cons

2 views
Skip to first unread message

Dirt com>

unread,
Jan 20, 2000, 3:00:00 AM1/20/00
to
Hiya,
Is there a way to create the following list using 'cons'

( ( (a (b (x) d) ) ) )

And if so, could you please show me how?

Thank you in advance,
Dirt

Dirt

unread,
Jan 20, 2000, 3:00:00 AM1/20/00
to
Hey,
Thanks alot, much appreciated. I'm glad I asked about that.

I had a similar problem;
( a ( b (x d) ) )

and my solution was:
( cons 'a ( cons ( cons 'b ( cons' (xd) nil ) ) nil ) )

I take it that this is not the correct way to go about doing this.
Should I be able to do this with one cons?

Thanks,
Dirt

On 21 Jan 2000 00:06:51 +0000, Erik Naggum <er...@naggum.no> wrote:


Erik Naggum

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
* Dirt <pi...@rochester.rr<.>com>

| Is there a way to create the following list using 'cons'
|
| ( ( (a (b (x) d) ) ) )

sure. (cons '((a (b (x) d))) nil)

you take your homework from there.

#:Erik

Johan Kullstam

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
Dirt <pi...@inam3.com> writes:

> Hey,
> Thanks alot, much appreciated. I'm glad I asked about that.
>
> I had a similar problem;
> ( a ( b (x d) ) )
>
> and my solution was:
> ( cons 'a ( cons ( cons 'b ( cons' (xd) nil ) ) nil ) )
>
> I take it that this is not the correct way to go about doing this.
> Should I be able to do this with one cons?

you need more than one cons. one is not enough.

--
J o h a n K u l l s t a m
[kull...@ne.mediaone.net]
Don't Fear the Penguin!

Tim Bradshaw

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
* Dirt wrote:

> I take it that this is not the correct way to go about doing this.
> Should I be able to do this with one cons?

You can create any list using one call to CONS if you give it the
right arguments. If you want to start with non-cons arguments then
there is a considerably more limited range of lists you can construct.
Specifically (x) for x any non-cons. (note I am counting only proper
lists).

--tim

Michael Kappert

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to

Tim Bradshaw wrote:
>
> You can create any list using one call to CONS if you give it the
> right arguments.

You can create any list without using cons if you give the right
arguments to the lisp reader, no?


> If you want to start with non-cons arguments then
> there is a considerably more limited range of lists you can construct.
> Specifically (x) for x any non-cons. (note I am counting only proper
> lists).

Hm. I think i need lessons in basic lisp terminology?

USER(1): (not (consp nil))
T
USER(2): (not (consp (quote x)))
T
USER(3): (cons (quote x) nil)
(X)
USER(4): (cons (quote x) (quote x))
(X . X)


-Michael.

--

Tony

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
Using multiple conses is not the best way to create a list
of this type. You are better off using other functions to
prepare your data for the cons i.e.

(cons 'a (list (cons 'b (list (list 'x)))))

> (a (b (x)))

this may not look any better but is more efficient


* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping. Smart is Beautiful

Tim Bradshaw

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
* Michael Kappert wrote:

> You can create any list without using cons if you give the right
> arguments to the lisp reader, no?

The lisp reader almost certainly calls (something that calls) CONS.

> USER(1): (not (consp nil))
> T
> USER(2): (not (consp (quote x)))
> T
> USER(3): (cons (quote x) nil)
> (X)
> USER(4): (cons (quote x) (quote x))
> (X . X)

That's not a `proper list' since its last CDR is not NIL. If you want
to include non-proper lists then you can obviously make:

(x . y)

for any x or y not a cons (and this includes (x . NIL) of course.

--tim

Michael Kappert

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
Tim Bradshaw wrote:

> * Michael Kappert wrote:

> > You can create any list without using cons if you give the right
> > arguments to the lisp reader, no?

> The lisp reader almost certainly calls (something that calls) CONS.

From a previous post:

* Tim Bradshaw:


| You can create any list using one call to CONS if you give it the
| right arguments.

But how do you arrive at the right arguments? If you already did use CONS,
then of course, you don't need to use it anymore :-)

> > USER(1): (not (consp nil))
> > T
> > USER(2): (not (consp (quote x)))
> > T
> > USER(3): (cons (quote x) nil)
> > (X)
> > USER(4): (cons (quote x) (quote x))
> > (X . X)
>
> That's not a `proper list' since its last CDR is not NIL.

I didn't say it was *pout*
From a previous post:

* Tim Bradshaw:


| If you want to start with non-cons arguments then
| there is a considerably more limited range of lists you can construct.

I still don't see which lists you can't construct using only CONS and starting
with
non-cons arguments?

-Michael.

--

Tim Bradshaw

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to
* Michael Kappert wrote:

> I still don't see which lists you can't construct using only CONS and starting
> with
> non-cons arguments?

If you are restricted to proper lists you can construct the list (<x>)
for any non-cons <x>.

--tim

Mayer Goldberg

unread,
Jan 21, 2000, 3:00:00 AM1/21/00
to

On 21-Jan-2000, Michael Kappert <k...@iitb.fhg.de> wrote:

> But how do you arrive at the right arguments? If you already did use CONS,
> then of course, you don't need to use it anymore :-)

Maybe the following procedure can help:

(defun build (s)
(cond ((consp s)
`(cons ,(build (car s))
,(build (cdr s))))
((member s '(t nil)) s)
((symbolp s) `',s)
(t s)))

Now try:

> (build '(a b c))
(CONS 'A (CONS 'B (CONS 'C NIL)))
> (build '(a t nil c))
(CONS 'A (CONS T (CONS NIL (CONS 'C NIL))))

Hope this helps.

Mayer

Christopher R. Barry

unread,
Jan 27, 2000, 3:00:00 AM1/27/00
to
Dirt <pi...@rochester.rr<.>com> writes:

> Hiya,


> Is there a way to create the following list using 'cons'
>
> ( ( (a (b (x) d) ) ) )
>

> And if so, could you please show me how?
>
> Thank you in advance,

(defun print-with-dots (list)
(cond ((atom list)
(princ list))
(t
(princ "(")
(print-with-dots (car list))
(princ " . ")
(print-with-dots (cdr list))
(princ ")")))
(values))

(print-with-dots '(((a (b (x) d)))))
=> (((A . ((B . ((X . NIL) . (D . NIL))) . NIL)) . NIL) . NIL)


(defun print-with-cons (list)
(cond ((atom list)
(princ list))
(t
(princ "(cons ")
(print-with-cons (car list))
(princ " ")
(print-with-cons (cdr list))
(princ ")")))
(values))

(print-with-cons '(((a (b (x) d)))))
=> (cons (cons (cons A (cons (cons B (cons (cons X NIL) (cons D NIL))) NIL)) NIL) NIL)


Christopher

0 new messages