First 3 elements of list

Discussion in 'AutoCAD' started by BillZ, Jun 25, 2004.

  1. BillZ

    BillZ Guest

    R2005 Vlisp:

    How can I get the first 3 items of a list.

    (18.1876 5.3017 -9.86971 0.0)

    Besides (list (car lt)(cadr lt)(caddr lt))

    TIA

    Bill
     
    BillZ, Jun 25, 2004
    #1
  2. BillZ

    Steve Doman Guest

    Bill,

    Here's one way that works for a list with 4 elements:

    (setq a (18.1876 5.3017 -9.86971 0.0))
    (reverse (cdr (reverse a)))

    But I prefer the example you posted.

    Steve Doman
     
    Steve Doman, Jun 25, 2004
    #2
  3. BillZ

    Jon Guest

    I prefer nth per
    (list
    (nth 0 lt)
    (nth 1 lt)
    (nth 2 lt)
    )
     
    Jon, Jun 25, 2004
    #3
  4. BillZ

    Joe Burke Guest

    Bill,

    I agree with Steve. Why not (list (car... ?

    An alternate method given a list of unknown length.

    (setq lst '(18.1876 5.3017 -9.86971 0.0 0.0 0.0))
    (setq newlst nil)
    (while (< (length newlst) 4)
    (setq newlst (cons (car lst) newlst))
    (setq lst (cdr lst))
    )
    (reverse newlst)

    Joe Burke
     
    Joe Burke, Jun 25, 2004
    #4
  5. BillZ

    zeha Guest

    Also

    (mapcar '(lambda(e1 e2) e2)'(1 2 3)'(18.1876 5.3017 -9.86971 0.0))
     
    zeha, Jun 25, 2004
    #5
  6. BillZ

    BillZ Guest

    That's what I was looking for.

    Interesting technique also. :)


    Thanks

    Bill
     
    BillZ, Jun 25, 2004
    #6
  7. BillZ

    BillZ Guest

    Bill,
    Usually when I ask here, I find a "slicker" way to do what I thought was the only way to do it, for years....

    Bill
     
    BillZ, Jun 25, 2004
    #7
  8. Although I'd hate to maintain that code. Imagine trying to read that 2 years
    from now and figure out what is the *real* intention of the statement.

    --
    R. Robert Bell


    That's what I was looking for.

    Interesting technique also. :)


    Thanks

    Bill
     
    R. Robert Bell, Jun 25, 2004
    #8
  9. BillZ

    Joe Burke Guest

    I thought what I posted was the long way around.

    Mapcar for something like this? There's probably a few other tortured methods
    available. Let's have a contest. The slowest method wins. ;-)

    Joe Burke
     
    Joe Burke, Jun 25, 2004
    #9
  10. BillZ

    Barr Guest

    I vote for Steve's; only 24 characters.

     
    Barr, Jun 25, 2004
    #10
  11. BillZ

    Barr Guest

    ....for fastest, that is.
     
    Barr, Jun 25, 2004
    #11
  12. BillZ

    BillZ Guest

    Although I'd hate to maintain that code. Imagine trying to >>read that 2 years
    Good point,
    '((car lt)(cadr lt)(caddr lt))
    is probably best anyway.
    I've always been a sucker for gimmicks. :)

    Bill
     
    BillZ, Jun 25, 2004
    #12
  13. Don't know about faster, but this is another way:

    (defun list-head (lst cnt)
    (cond
    ( (or (zerop cnt) (not lst)) nil)
    (t (cons (car lst)
    (list-head (cdr lst) (1- cnt))
    )
    )
    )
    )



    AutoCAD based Security Planning Solutions:
    http://www.caddzone.com/securityplanning
     
    Tony Tanzillo, Jun 25, 2004
    #13
  14. I rarely use the mapcar and lambda (I know, please no chastisements).
    So, this took a while to figure out, but here is another method -

    (setq Glist (list "1a" "2b" "3c" "4d" "5e"))
    (defun GetList (Nlist Glist /)
    (mapcar '(lambda (x) (nth x Glist)) Nlist)
    )

    Command: (GetList (list 0 1) Glist)
    ("1a" "2b")

    Command: (GetList (list 0 1 2 3) Glist)
    ("1a" "2b" "3c" "4d")

    Command: (GetList (list 1 3) Glist)
    ("2b" "4d")

    Command: (GetList (list 0) Glist)
    ("1a")
     
    Alan Henderson @ A'cad Solutions, Jun 25, 2004
    #14
  15. How's this?

    (defun 3_of_list (flist / nlist)
    (repeat 3
    (setq nlist (cons (car flist) nlist)
    flist (cdr flist))
    )
    (reverse nlist)
    )

    --
    Ken Alexander
    Acad2004
    Windows XP

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jun 25, 2004
    #15
  16. BillZ

    Adesu Guest

    How about this
    (vl-string-right-trim "0.0" "18.1876 5.3017 -9.86971")
     
    Adesu, Jun 28, 2004
    #16
  17. BillZ

    Doug Broad Guest

    Bill,
    There's nothing wrong with your first post and Tony's
    function is a good way to generalize the sitiuation but
    your last post won't do anything. The quote will stop
    evaluation.
    Regards,
    Doug
     
    Doug Broad, Jun 28, 2004
    #17
  18. BillZ

    BillZ Guest

    Doug,
    thanks.
    I am having trouble understanding quote.
    Seems like sometimes I can use '(( to list things and sometimes I have to use (list (.

    Bill
     
    BillZ, Jun 28, 2004
    #18
  19. BillZ

    Doug Broad Guest

    Just remember:
    If you don't want the list evaluated, use quote.

    '(1 2 3 4) is OK because literal numbers (and strings)
    do not require evaluation.

    (list 1 2 3 4) is OK also.

    (setq a 1 b 2 c 3)
    3

    '(a b c) returns (a b c)
    (list a b c) returns (1 2 3)

    (mapcar 'eval '(a b c)) returns (1 2 3) [not recommended, just an illustration]
     
    Doug Broad, Jun 28, 2004
    #19
  20. BillZ

    BillZ Guest

    Hey thanks,

    I kinda thought that's the way it worked. :)

    Bill
     
    BillZ, Jun 28, 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.