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

Daron Denton

Usenet Archive
Joined
Jul 21, 2003
Messages
55
Reaction score
0
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?
 
(defun C:TEST
(setq str "")
(foreach a '("aaa" "bbb")
(foreach b '("BBB" "CCC" "DDD")
(setq str (strcat str a b ","))
)
)
)
 
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"))))
)
 
Why are you trying to process without the foreach?

Devin
 
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
 
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.
 
Very minor change to your original:

(apply 'strcat
(mapcar '(lambda (y)
(apply 'strcat
(mapcar '(lambda (x)
(strcat y x ","))
'("BBB" "CCC" "DDD"))
))
'("aaa" "AAA")
)
)
 
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
 

Latest threads

Back
Top