# Thread: Change a list of a list.. tginking to difficult?

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

Registered forum members do not see this ad.

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

I got the following:
Code:
`!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
Code:
`!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...

2. 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.

3. Without using append:
Code:
```_\$ (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:
Code:
```_\$ (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))```

4. 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):
Code:
```; _\$ (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".

5. Thanks guys,
works like a charm!

I was very close with Lee's solution:
Code:
`(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!

6. 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:

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

(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)))) )
)
)

(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)))) )
)
)```
Code:
```(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))
(addlastatom 45 '((1 1)(1 2)(1 3 (8 4 (7 4))))) >> ((1 1) (1 2) (1 3 (8 4 (4 7 45))))
(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.

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

8. Originally Posted by BIGAL
Grr double check 7 4 now 4 7 45 should be 7 4 45
Doh, thanks for pointing that out, BIGAL !
Fixed now...

9. I think you can do away with a few of those test expressions...
Code:
```(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))))
)
)```

10. Registered forum members do not see this ad.

Originally Posted by Lee Mac
I think you can do away with a few of those test expressions...
<code>
Thanks Lee,
Sometimes when I get confused the cond helps me splitting the problems.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts