a better way - apply/mapcar/lambda/mapcar/lambda

Discussion in 'AutoCAD' started by Daron Denton, Jul 21, 2003.

  1. Daron Denton

    Daron Denton Guest

    school me please.
    I want to prefix '("BBB" "CCC" "DDD") with '("aaa" "AAA"), and combine as
    one string, comma delimited.

    (apply 'strcat
    (apply 'append
    (mapcar '(lambda (A )
    (mapcar '(lambda (X )
    (strcat A X ",")
    )
    '("BBB" "CCC" "DDD")
    )
    )
    '("aaa" "AAA")
    )))

    correctly returns "aaaBBB,aaaCCC,aaaDDD,AAABBB,AAACCC,AAADDD,"

    Is there a more elegant way of doing this?
     
    Daron Denton, Jul 21, 2003
    #1
  2. Daron Denton

    Devin Guest

    (defun C:TEST ()
    (setq str "")
    (foreach a '("aaa" "bbb")
    (foreach b '("BBB" "CCC" "DDD")
    (setq str (strcat str a b ","))
    )
    )
    )
     
    Devin, Jul 21, 2003
    #2
  3. Don't know if it's any better.......

    (setq newlist '())
    (foreach x '("aaa" "AAA")
    (setq newlist
    (append newlist
    (mapcar
    '(lambda (y)
    (strcat x y ","))
    '("BBB" "CCC" "DDD"))))
    )

    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jul 21, 2003
    #3
  4. Daron Denton

    Devin Guest

    Why are you trying to process without the foreach?

    Devin
     
    Devin, Jul 22, 2003
    #4
  5. Daron Denton

    Daron Denton Guest

    mapcar processes lists faster than foreach
     
    Daron Denton, Jul 22, 2003
    #5
  6. Daron Denton

    Devin Guest

    I didn't know that, thanks!

    Devin
     
    Devin, Jul 22, 2003
    #6
  7. Daron Denton

    Devin Guest

    Daron,

    You might want to do a timing check, just to see which is faster in this
    case. The other way seems to add steps in the process, which my increase
    the runtime required.

    Devin
     
    Devin, Jul 22, 2003
    #7
  8. Daron Denton

    Daron Denton Guest

    There has been much of that here in the past. That's why i posted, hoping
    one of the list pro's would chime in. Mine is overkill for the example, but
    it's been simplified here for testing. When the '("BBB" "CCC" "DDD") list
    contains over 100 items, it makes a difference.
     
    Daron Denton, Jul 22, 2003
    #8
  9. Daron Denton

    Devin Guest

    I see.

    Devin
     
    Devin, Jul 22, 2003
    #9
  10. Very minor change to your original:

    (apply 'strcat
    (mapcar '(lambda (y)
    (apply 'strcat
    (mapcar '(lambda (x)
    (strcat y x ","))
    '("BBB" "CCC" "DDD"))
    ))
    '("aaa" "AAA")
    )
    )

    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Jul 22, 2003
    #10
  11. I believe that depends on the list.
     
    Jason Piercey, Jul 22, 2003
    #11
  12. Daron Denton

    Devin Guest

    As I seem to be learning. I found that when I switched to mapcar my list
    processing was much faster. I wonder why it's foreach is slower?

    Devin
     
    Devin, Jul 22, 2003
    #12
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.