Jump to content
Aftertouch

Change a list of a list.. tginking to difficult?

Recommended Posts

Aftertouch

Hello all.

Got myself a problem wich is cracking my head...

 

I got the following:

!test ((1 1)(1 1)(1 1))

 

How can i add a number to the last list of the list?

Numbers could be anything and could also be just one list in list. So im looking to get

!test ((1 1)(1 1)(1 1 2))

 

I was thinking to..

Reverse the list.

Get the first of the list.

Use cdr to get second and other parts.

Cons the first part wich the added value again.

 

But this will fail when the list is just one list long...

Share this post


Link to post
Share on other sites
BIGAL

I tend to use nth function on a list (nth 2 test) = (1 1) then use nth again and a cons to make a new item (1 1 2) then cons the remainder of the list using nth to a new list lastly test = newlist.

 

Dont forget (setq x (length test)) that the list starts at 0 not 1 so using a repeat with (nth (setq x (- x 1)) will step through the list.

 

You could do something like enter item number or L last, add item, subtrcat item, replace item a more global list manipulator.

Share this post


Link to post
Share on other sites
Lee Mac

Without using append:

_$ (setq lst '((1 1)(1 1)(1 1)))
((1 1) (1 1) (1 1))
_$ (reverse (cons (reverse (cons 2 (reverse (car (reverse lst))))) (cdr (reverse lst))))
((1 1) (1 1) (1 1 2))

Using append:

_$ (setq lst '((1 1)(1 1)(1 1)))
((1 1) (1 1) (1 1))
_$ (append (reverse (cdr (reverse lst))) (list (append (last lst) '(2))))
((1 1) (1 1) (1 1 2))

Share this post


Link to post
Share on other sites
Grrr

Usually I'd use Lee's first suggestion, but theres another (inefficient) way to reverse the whole list and its subitems, then unreverse (reverse it back):

; _$ (addlast 45 '((1 1)(1 2)(1 3))) -> ((1 1) (1 2) (1 3 45))
(defun addlast ( x L )
 ( (lambda (x L) (reverse (mapcar 'reverse (cons (cons x (car L)) (cdr L)))))
   x
   (reverse (mapcar 'reverse L))
 )
)

 

Just trying to contribute on "another way to do this".

Edited by Grrr

Share this post


Link to post
Share on other sites
Aftertouch

Thanks guys,

works like a charm!

 

I was very close with Lee's solution:

(reverse (cons (reverse (cons 2 (reverse (car (reverse lst))))) (cdr (reverse lst))))

 

But i didnt know cdr would return nill when then list is just '((1 1)).

I tought it would return an error.

 

Thanks again guys!

Share this post


Link to post
Share on other sites
Grrr

Yea, I know this thread is getting old...

However just now decided to go back and think of this as recursive problem, so this might help someone:

 

(defun addfirstatom ( x L )
 (cond 
   ( (not L) nil)
   ( (atom (car L)) (cons x L) )
   ( (listp L) (cons (addfirstatom x (car L)) (cdr L)) )
 )
)

[s](defun addlastatom ( x L )
 (cond 
   ( (not L) nil)
   ( (atom (last L)) (reverse (cons x L)) )
   ( (listp L) (append (reverse (cdr (reverse L))) (list (addlastatom x (last L)))) )
 )
)[/s]

(defun addlastatom ( x L )
 (cond 
   ( (not L) nil)
   ( (atom (last L)) (append L (list x)) )
   ( (listp L) (append (reverse (cdr (reverse L))) (list (addlastatom x (last L)))) )
 )
)

 

(addfirstatom 45 '((((3 6) 1 2 3) 1 1)(1 2)(1 3))) >> ((((45 3 6) 1 2 3) 1 1) (1 2) (1 3))
[s](addlastatom 45 '((1 1)(1 2)(1 3 (8 4 (7 4))))) >> ((1 1) (1 2) (1 3 (8 4 (4 7 45))))[/s]
(addlastatom 45 '((1 1)(1 2)(1 3 (8 4 (7 4))))) >> ((1 1) (1 2) (1 3 (8 4 (7 4 45))))

 

...and now its gone in my archive of recursions. :lol:

Edited by Grrr

Share this post


Link to post
Share on other sites
BIGAL

Grr double check 7 4 now 4 7 45 should be 7 4 45

Share this post


Link to post
Share on other sites
Grrr
Grr double check 7 4 now 4 7 45 should be 7 4 45

 

Doh, thanks for pointing that out, BIGAL !

Fixed now...

Share this post


Link to post
Share on other sites
Lee Mac

I think you can do away with a few of those test expressions...

(defun conscar ( x l )
   (if (atom (car l))
       (cons x l)
       (cons (conscar x (car l)) (cdr l))
   )
)
(defun conslast ( x l )
   (if (atom (last l))
       (append l (list x))
       (reverse (cons (conslast x (last l)) (cdr (reverse l))))
   )
)

Share this post


Link to post
Share on other sites
Grrr
I think you can do away with a few of those test expressions...

 

Thanks Lee,

Sometimes when I get confused the cond helps me splitting the problems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×