Mapcar + lambda Description
Hi GUYS.I have read the Help which attached with Autocad and Afralisp explainations about
mapcar and lambda but without a chance to understand.
So if someone could help me with it, It would be very kind favor.
Thankxxxxxxx
Sweety. Mapcar combines lists, or can take an action on a list - in either event it (mapcar) returns the result as a list.
Lambda is an anonymous function within itself, which does not require a defun.
Often times, mapcar and lambda are used together to yield a desired result... Apply and lambda can also be combined, but lambda can be used alone as well.
Do you have something specific you want to code, or was this just a general inquiry? Thanks Renderman.
You are always in the first .
I have a code but it's not mine and do not want to post to avoid any screw up from anyone.
So in general like this one .... I hope it has written right .
(mapcar '(lambda (x y) (+ (x y) 1.0) '(pt1 pt2)))
Question. how (x y) would be added to each other since they have no values .
If you add more explainations, It would be a very kind of you as usual.
Thankxxxxxxxxxxxxxxxxxxxxxxxxxx.
Thanks.
But it's not open for public .
I have no username in that Forum. The way I see it, here is the crux of it, mapcar evaluates afunction on every element of a list and returns a list containing theresult of each such evaluation. The function can be any function, takingany number of arguments. lambda describes an anonymous function(usually used for a one-off usage, hence not warranting the overhead of a'defun'), and, as a function, can be supplied as an argument to mapcar.
Examples are the best way to learn:
;; Add 1 to every element of a list:(mapcar '1+ '(1 2 3 4 5))==>'(2 3 4 5 6)Note that the function argument is quoted (using the apostrophe), as it is tobe taken as an argument for mapcar and hence not evaluated. Theapostrophe means, in laymans terms: take this statement at face value anddon't evaluate it.
More Examples:
(defun AddTwo ( x ) (+ x 2))(mapcar 'AddTwo '(1 2 3 4 5))==> (3 4 5 6 7)(mapcar '(lambda ( x ) (+ x 2)) '(1 2 3 4 5))==> (3 4 5 6 7)
(mapcar '+ '(1 2 3 4 5) '(3 4 5 6 7))==> (4 6 8 10 12) = ((+ 1 3) (+ 2 4) (+ 3 5) (+ 4 6) (+ 5 7))
(defun foo ( string number ) (strcat string (itoa number)))(mapcar 'foo '("a" "b" "c") '(1 2 3))("a1" "b2" "c3")(mapcar '(lambda ( string number ) (strcat string (itoa number))) '("a" "b" "c") '(1 2 3))("a1" "b2" "c3")And using Apply:
(defun averageof2 ( num1 num2 ) (/ (+ num1 num2) 2.))(apply 'averageof2 '( 1 2 ))==> 1.5(apply '(lambda ( num1 num2 ) (/ (+ num1 num2) 2.)) '( 1 2 ))==> 1.5Or, for a generic average function perhaps:
(defun average ( lst ) (/ (apply '+ lst) (float (length lst))))(average '( 1 2 3 4 ))==>2.5Or, here's one to mess with your head, average of a list of points:
(defun AveragePoint ( l ) (mapcar '(lambda ( x ) (/ x (float (length l)))) (apply 'mapcar (cons '+ l))))(averagepoint '(( 2 3 1 ) ( 2 4 1 ) ( 1 5 1 )))==> (1.66667 4.0 1.0)Lee Great examples, Lee (as usual). :wink:
The only thing I would add, is that another major 'appeal' to using mapcar is that it can handle a list of any length.
Sometimes you might only have a handful of items in your list, other times it could be +1000... in either case, mapcar (along with apply) can handle the job. Wow
I was shocked with these examples, they are really very helpful.
So I have to clean up my mind to start studing these examples.
Thank you so much LEE. Happy to help out, if you have any questions about my examples, just ask
Dear Mr.Lee,
This is perfect examples and you are really great and it will helpful to everybody even if they know about mapcar.
Thank you once again for your great effort.
Thanks Muthu for your kind words, I'm happy that many members may benefit from my post
页:
[1]
2