+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Senior Member
    Discipline
    Civil
    Aftertouch's Discipline Details
    Discipline
    Civil
    Details
    Engineer
    Using
    AutoCAD 2017
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    258

    Default 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. #2
    Quantum Mechanic
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    10,017

    Default

    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.
    A man who never made mistakes never made anything

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,408

    Default

    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))
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  4. #4
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,215

    Default

    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".
    Last edited by Grrr; 11th Dec 2017 at 02:42 am.

  5. #5
    Senior Member
    Discipline
    Civil
    Aftertouch's Discipline Details
    Discipline
    Civil
    Details
    Engineer
    Using
    AutoCAD 2017
    Join Date
    Jul 2016
    Location
    Netherlands
    Posts
    258

    Default

    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. #6
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,215

    Default

    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.
    Last edited by Grrr; 13th Jan 2018 at 12:21 pm.

  7. #7
    Quantum Mechanic
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    10,017

    Default

    Grr double check 7 4 now 4 7 45 should be 7 4 45
    A man who never made mistakes never made anything

  8. #8
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,215

    Default

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

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,408

    Default

    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))))
        )
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  10. #10
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,215

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Lee Mac View Post
    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.

Similar Threads

  1. Change list by Lisp.
    By Huuthanh in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 18th Jan 2014, 08:21 pm
  2. List of the file list (only names) copy to excel
    By wimal in forum AutoCAD General
    Replies: 4
    Last Post: 3rd Jul 2013, 11:53 am
  3. generate part-list/order-list in autocad 2011LT
    By Borken in forum AutoCAD Beginners' Area
    Replies: 7
    Last Post: 12th Jul 2012, 09:39 am
  4. write a list into text file and return back as list
    By muthu123 in forum AutoLISP, Visual LISP & DCL
    Replies: 14
    Last Post: 21st Dec 2009, 04:31 pm
  5. Change popup list contents from one list to another
    By The Buzzard in forum AutoLISP, Visual LISP & DCL
    Replies: 9
    Last Post: 4th Mar 2009, 05:22 am

Bookmarks

Posting Permissions

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