Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Blog: Mapcar: "Help" didn't tell me . . .

6 REPLIES 6
Reply
Message 1 of 7
Anonymous
154 Views, 6 Replies

Blog: Mapcar: "Help" didn't tell me . . .

Help says mapcar applies a function to each element in a list, resulting in a list:

(mapcar '1+ '(1 2 3)) = '(2 3 4)
or (mapcar (lambda (n) (+ n 2)) '(1 2 3)) = '(4 5 6)

Well and good. But here's another use (which I'm sure everyone here but me already knows):

(mapcar '+ '(2 3 4) '(6 7 8)) = '(8 10 12)

Well, durn. I can't see this in "Help." But is is Very important. Can you explain the
logical leap between {1} and {2}? Did everybody but me notice this right away?

For fun I worked out a distance function (ok, I know there is one in the box):

(defun #dst2(L1 L2 / )
(expt(apply '+ (mapcar '* (mapcar '+ L1 L2)(mapcar '+ L1 L2))) 0.5)
) ;defun

Thanks for listening. I feel much better now.

rs
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: Anonymous

Bob,

I might be wrong, but I think your question is related to passing one or two lists to
mapcar.

When one list is passed, the argument function is applied to that list. When two
lists are passed, the argument function is applied each item in each list in turn.

So this (mapcar '+ '(2 3 4) '(6 7 8)) = '(8 10 12) makes sense.

It says add 2 to 6, add 3 to 7, and add 4 to 8.

Joe Burke

"tcebob" wrote in message
news:5682006@discussion.autodesk.com...
Help says mapcar applies a function to each element in a list, resulting in a list:

(mapcar '1+ '(1 2 3)) = '(2 3 4)
or (mapcar (lambda (n) (+ n 2)) '(1 2 3)) = '(4 5 6)

Well and good. But here's another use (which I'm sure everyone here but me already
knows):

(mapcar '+ '(2 3 4) '(6 7 8)) = '(8 10 12)

Well, durn. I can't see this in "Help." But is is Very important. Can you explain
the
logical leap between {1} and {2}? Did everybody but me notice this right away?

For fun I worked out a distance function (ok, I know there is one in the box):

(defun #dst2(L1 L2 / )
(expt(apply '+ (mapcar '* (mapcar '+ L1 L2)(mapcar '+ L1 L2))) 0.5)
) ;defun

Thanks for listening. I feel much better now.

rs
Message 3 of 7
_gile
in reply to: Anonymous

Hi,

In your #dst2 function, i suppose you meant (mapcar '- L1 L2) rather than (mapcar '+ L1 L2) ?

You could also replace the 3 successive mapcar by only one with a lambda function :

(defun my-dist (p1 p2)
(sqrt (apply '+ (mapcar '(lambda (x1 x2) (expt (- x1 x2) 2)) p1 p2)))
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 7
Tom Smith
in reply to: Anonymous

Bob, in 07 it says "Returns a list that is the result of executing a function with a list (or lists) supplied as arguments to the function," which is a little more clear.
Message 5 of 7
Anonymous
in reply to: Anonymous

"tcebob" wrote in message
news:5682006@discussion.autodesk.com...

Did everybody but me notice this right away?


- I can only speak for myself, but I must say that it took me much time to
see *some* of the nice uses of mapcar. (Now and then the light bulb goes
on.) (Learned AutoLisp OTJ and via a few books (and help files), but
*mostly* from the many helpful posts here.) Although I've generally been
able to write code sufficient for our needs at my current company (and
previous places of employment), and though I have been fiddling with
AutoLISP since ~1997, I would still consider myself a 'novice' programmer,
since 1) I am extremely limited timewise for any major programming
efforts - even though I would like to do more; 2) I haven't taken any
formal programming classes; and 3) I haven't been able to expand my grasp
of other programming languages. Most of what I write is to fulfill a
specific drafting need, which doesn't add much to any marketability, (other
than my own).

Would I like things to be different? For sure I'd like to do more
programming. But for now I'll just have to have to be content and take it
as it comes. 🙂

Best regards,
David Kozina
Message 6 of 7
Tom Smith
in reply to: Anonymous

For me, mapcar is one of those things that's easier to understand by example than by a definition.

I don't remember how my R9 lisp manual described it, but very early on I saw an example using mapcar to add points or vectors, and besides being a very useful application, it made me aware of Bob's second case.
Message 7 of 7
Anonymous
in reply to: Anonymous

1. Correct.

2. Yes, but I was trying to illustrate this new and exciting capability.

Thanks for the response.

rs


wrote in message news:5682093@discussion.autodesk.com...
Hi,

In your #dst2 function, i suppose you meant (mapcar '- L1 L2) rather than (mapcar '+ L1
L2) ?

You could also replace the 3 successive mapcar by only one with a lambda function :

(defun my-dist (p1 p2)
(sqrt (apply '+ (mapcar '(lambda (x1 x2) (expt (- x1 x2) 2)) p1 p2)))
)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost