From: Dirt
Subject: [Q]: Need help with cons
Date: 
Message-ID: <tn7f8skumiviodikhv0esp7m88pbddsod1@4ax.com>
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

From: Erik Naggum
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <3157402011629405@naggum.no>
* Dirt <····@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
From: Dirt
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <hbdf8so8km1bsprkdbvfb1nt3fhe8akr0f@4ax.com>
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 <····@naggum.no> wrote:
From: Johan Kullstam
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <m2ogagrxqh.fsf@sophia.axel.nom>
Dirt <····@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
[········@ne.mediaone.net]
Don't Fear the Penguin!
From: Tim Bradshaw
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <ey3snzs5cfv.fsf@cley.com>
* 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
From: Michael Kappert
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <38882087.ECAF27E6@iitb.fhg.de>
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.

--
From: Tim Bradshaw
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <ey3iu0n60t6.fsf@cley.com>
* 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
From: Michael Kappert
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <38887BC8.F5072B4B@iitb.fhg.de>
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.

--
From: Tim Bradshaw
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <ey31z7b5q60.fsf@cley.com>
* 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
From: Mayer Goldberg
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <86aklf$n8a$1@lnews.actcom.co.il>
On 21-Jan-2000, Michael Kappert <···@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
From: Tony
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <01dfed54.fd64fa89@usw-ex0109-066.remarq.com>
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
From: Christopher R. Barry
Subject: Re: [Q]: Need help with cons
Date: 
Message-ID: <87aelskk18.fsf@2xtreme.net>
Dirt <····@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