( ( (a (b (x) d) ) ) )
And if so, could you please show me how?
Thank you in advance,
Dirt
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:
sure. (cons '((a (b (x) d))) nil)
you take your homework from there.
#:Erik
> 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!
> 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
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.
--
(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
> 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 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.
--
> 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
> 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
> 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