Jump to content

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


Aftertouch

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 1 month later...

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...