Aftertouch Posted December 10, 2017 Share Posted December 10, 2017 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... Quote Link to comment Share on other sites More sharing options...
BIGAL Posted December 10, 2017 Share Posted December 10, 2017 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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted December 10, 2017 Share Posted December 10, 2017 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)) Quote Link to comment Share on other sites More sharing options...
Grrr Posted December 11, 2017 Share Posted December 11, 2017 (edited) 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 December 11, 2017 by Grrr Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted December 11, 2017 Author Share Posted December 11, 2017 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! Quote Link to comment Share on other sites More sharing options...
Grrr Posted January 12, 2018 Share Posted January 12, 2018 (edited) 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. Edited January 13, 2018 by Grrr Quote Link to comment Share on other sites More sharing options...
BIGAL Posted January 13, 2018 Share Posted January 13, 2018 Grr double check 7 4 now 4 7 45 should be 7 4 45 Quote Link to comment Share on other sites More sharing options...
Grrr Posted January 13, 2018 Share Posted January 13, 2018 Grr double check 7 4 now 4 7 45 should be 7 4 45 Doh, thanks for pointing that out, BIGAL ! Fixed now... Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted January 13, 2018 Share Posted January 13, 2018 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)))) ) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted January 13, 2018 Share Posted January 13, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.