how to covert list

Discussion in 'AutoCAD' started by kemp, Apr 6, 2004.

  1. kemp

    kemp Guest

    What functions should i be looking for to convert a list in the format:

    ("A") ("B") ("C") ("D")

    to the format:

    ("A" "B" "C" "D")

    It's probably something simple but I'm still learning :)

    Thanks!
     
    kemp, Apr 6, 2004
    #1
  2. kemp

    Tom Smith Guest

    Or (append '("A") '("B") '("C") '("D")).

    You're not converting a list, you're merging 4 lists into one.
     
    Tom Smith, Apr 6, 2004
    #2
  3. kemp

    Devin Guest

    Or rather (apply 'append '(1 2 3 4 5))
     
    Devin, Apr 6, 2004
    #3
  4. kemp

    Tom Smith Guest

    Or rather (apply 'append '(1 2 3 4 5))

    That isn't valid, and it doesn't address the original question, merging
    multiple lists into one.

    My suggestion (append '("A") '("B") '("C") '("D")) correctly evaluates to
    ("A" "B" "C" "D") which is what the poster wanted.
     
    Tom Smith, Apr 6, 2004
    #4
  5. kemp

    Tom Smith Guest

    $ (apply 'append '(("A") ("B") ("C") ("D")))
    That's correct if there is actually a list of lists to begin with -- the
    original post wasn't clear. I assumed the goal was to merge separate lists.
     
    Tom Smith, Apr 6, 2004
    #5
  6. kemp

    Devin Guest

    Silly me,

    Or rather (apply 'append '((a) (b) (c) (d)))

    The poster's question was directed at a single list per the statement:

    I guess I read between the lines, but how couldn't I when he said list and
    wrote ("A") ("B") ("C") ("D")?

    And yes I know your answers works and is good :) I was simply stating an
    alternative that his statement could have been addressing.
     
    Devin, Apr 6, 2004
    #6
  7. kemp

    kemp Guest

    Thanks everyone - You found more than one solution for me, now I get to play
    with 'em and see which I like best.

    kemp
     
    kemp, Apr 6, 2004
    #7
  8. kemp

    Doug Broad Guest

    Hi Luis and all,
    Now who would like to show how to flatten any nested list
    such as '((1 . 2) (1 2 3) ((1 2) (2 3) 4))

    into '(1 2 1 2 3 1 2 2 3 4)

    One issue would be should nil elements remain? I won't
    take a position on that one.

    Kudos for best iterative solution, best recursive solution,
    fastest and wittiest. Links to previous discussions are
    welcome.

    Regards,
    Doug
     
    Doug Broad, Apr 6, 2004
    #8
  9. kemp

    Devin Guest

    based on your example you could do this...

    (setq lst '((1 . 2) (1 2 3) ((1 2) (2 3) 4)))

    (defun en ( lst / pass )
    (traverse_list lst)
    pass
    )

    (defun traverse_list ( lsts / a p )
    (mapcar
    '(lambda (a)
    (if
    (listp a)
    (traverse_list
    (vl-remove-if 'null
    (append
    (list (car a))
    (if (listp (setq p (cdr a))) p (list p))
    )
    )
    )
    (progn
    (setq pass (append pass (list a)))
    a
    )
    )
    )
    lsts
    )
    )

    try (en lst)

    HTH,

    Devin
     
    Devin, Apr 6, 2004
    #9
  10. kemp

    Doug Broad Guest

    Looks good Devin. Lets see if anyone else has a different
    approach.


    <snip>
     
    Doug Broad, Apr 6, 2004
    #10
  11. This is simply a quick rewrite of an elegant recursive
    function posted by ____ _____ that used a cond structure;
    this one just uses if statements and makes one improvement
    on the original resulting in ~ 7% performance gain (very
    casual / quick test).

    (defun delist ( lst / x )
    (if lst
    (if (atom lst)
    (list lst)
    (if (atom (setq x (car lst)))
    (cons x (delist (cdr lst)))
    (append (delist x) (delist (cdr lst)))
    )
    )
    )
    )

    What's the one improvement?

    Does it still work?

    (delist '((1 . 2) (1 2 3) ((1 2) (2 3) 4)))

    (1 2 1 2 3 1 2 2 3 4)

    Just havin' fun :)

    PS: Gues who ____ _____ is?

    Hi Luis and all,
    Now who would like to show how to flatten any nested list
    such as '((1 . 2) (1 2 3) ((1 2) (2 3) 4))

    into '(1 2 1 2 3 1 2 2 3 4)

    One issue would be should nil elements remain? I won't
    take a position on that one.

    Kudos for best iterative solution, best recursive solution,
    fastest and wittiest. Links to previous discussions are
    welcome.

    Regards,
    Doug
     
    michael puckett, Apr 7, 2004
    #11
  12. kemp

    Joe Burke Guest

    Hi Michael,
    Doug, and it was called flatten.

    You think we don't save these gems, Doug? ;-)

    Jeo Burke
     
    Joe Burke, Apr 7, 2004
    #12
  13. Have you changed your name? ;) Struck me
    as funny when I read it :)
     
    Jason Piercey, Apr 7, 2004
    #13
  14. kemp

    Joe Burke Guest

    Jason,

    That's the name I use on my tax return. ;-)
     
    Joe Burke, Apr 7, 2004
    #14
  15. Truly a gem.

     
    michael puckett, Apr 7, 2004
    #15
  16. <snicker>
     
    Jason Piercey, Apr 7, 2004
    #16
  17. kemp

    Doug Broad Guest

    Thanks Guys,

    Must be early Alzheimer's. I had forgotten that I had
    posted one already.

    Michael. Thanks for posting your adaptation. I
    must have called it something else on my system
    cause my current flatten zaps the z coordinates on
    everything to 0.0.

    G'nite
     
    Doug Broad, Apr 7, 2004
    #17
  18. (defun flatten (lst)
    (cond ((null lst) nil)
    ((atom lst) (list lst))
    (T (append (flatten (car lst))
    (flatten (cdr lst))))))

    - sorry about possible indentation oddities, having trouble with fonts.

    --
     
    Martti Halminen, Apr 7, 2004
    #18
  19. The weird thing is mine is called delist for the very same reason!

    <xfiles.wav>

    Thanks Guys,

    Must be early Alzheimer's. I had forgotten that I had
    posted one already.

    Michael. Thanks for posting your adaptation. I
    must have called it something else on my system
    cause my current flatten zaps the z coordinates on
    everything to 0.0.

    G'nite
     
    michael puckett, Apr 7, 2004
    #19
  20. kemp

    Joe Burke Guest

    .... mine is renamed "flatlist" for the same reason.

    Martti's post is interesting. It looks like Doug's function, while leaving out his
    third condition. And it seems to work OK.

    Here's what I have from the original thread.

    ; D. C. Broad, Jr. 10/25/02 - was named "flatten" - renamed flatlist
    ; in list:
    ; '( 0 (1 2) ((3 . 4) (5 6)) (((7 8) (9 10 11 12)))
    ; ((((13 . 14) (15 16) (17 . 18) (19 20)))) (21)))
    ; out list:
    ; (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
    (defun flatlist (l)
    (cond
    ((null l) nil)
    ((atom l) (list l))
    ((atom (car l)) (cons (car l) (flatlist (cdr l))))
    ((append (flatlist (car l)) (flatlist (cdr l))))
    )
    )

    Joe Burke
     
    Joe Burke, Apr 7, 2004
    #20
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.