mapcar querry

mapcar querry

vishshreevT578L
Advocate Advocate
1,387 Views
14 Replies
Message 1 of 15

mapcar querry

vishshreevT578L
Advocate
Advocate
(setq a '(1 2 3 4 5) (1 2 3 4 5) In tutorials to add 1 to list a is (mapcar '1+ a) (2 3 4 5 6) but i want to add 2 to my list a please help
Shreev
0 Likes
Accepted solutions (1)
1,388 Views
14 Replies
Replies (14)
Message 2 of 15

Anonymous
Not applicable

Try

 

(mapcar '(lambda (x) (+ 2 x)) a)

 

^_^

Message 3 of 15

vishshreevT578L
Advocate
Advocate
Can you suggest me the website to start learning autolisp and vl
Shreev
0 Likes
Message 4 of 15

Anonymous
Not applicable

Ha ha,

 

This forum and google search to learn.

 

Search single funtion to learn.

 

^_^

0 Likes
Message 5 of 15

vishshreevT578L
Advocate
Advocate
Sir,
I understand i know just little of autolisp.
Please become a teacher and guide me.
So that i can also start writing small scripts.
Shreev
0 Likes
Message 6 of 15

Anonymous
Not applicable

You can search script in this forum.

 

Please search "word" before creat new topic.

 

^_^

0 Likes
Message 7 of 15

vishshreevT578L
Advocate
Advocate
Thanks
But is there any tutorial
Shreev
0 Likes
Message 8 of 15

Anonymous
Not applicable
Accepted solution

And 

 

search google what you need

 

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=autolisp%20tutorial

 

 

Keyword very importan.

 

^_^

0 Likes
Message 9 of 15

vishshreevT578L
Advocate
Advocate
Thank you sir
Shreev
0 Likes
Message 10 of 15

Anonymous
Not applicable

you're welcome.

and post your question if not found. 

^_^

 

Message 11 of 15

Kent1Cooper
Consultant
Consultant

@vishshreevT578L wrote:
(setq a '(1 2 3 4 5) (1 2 3 4 5) In tutorials to add 1 to list a is (mapcar '1+ a) (2 3 4 5 6) but i want to add 2 to my list a please help

In this specific case, another rather simple way to do it is to just add 1 to each one twice:

 

Command: (setq a '(1 2 3 4 5))
(1 2 3 4 5)

Command: (mapcar '1+ (mapcar '1+ a))
(3 4 5 6 7)

 

But that approach would get cumbersome quickly if you need to add larger numbers to those in the list -- @Anonymous's suggestion in Post 2 is really the way to go, because the only change would be in the 2, and the rest of the code doesn't need to get any longer.

Kent Cooper, AIA
0 Likes
Message 12 of 15

patrick_35
Collaborator
Collaborator

Hi

An another

 

(setq lst1 '(1 2 3 4 5))
(setq lst2 '(1 2 3 4 5))
(mapcar '(lambda(a b) (+ a b)) lst1 lst2)

@+

0 Likes
Message 13 of 15

Kent1Cooper
Consultant
Consultant

@patrick_35 wrote:

...

An another

 

(setq lst1 '(1 2 3 4 5))
(setq lst2 '(1 2 3 4 5))
(mapcar '(lambda(a b) (+ a b)) lst1 lst2)

....


That results in (2 4 6 8 10), adding each number to itself.  That result can be obtained more simply from those two lists:

 

(mapcar '+ lst1 lst2)

 

But to get what they're looking for -- adding 2 to each number in the original list [the equivalent of applying the (1+) function to each number as in Post 1, but adding 2 rather than 1] -- with this approach, the 'lst2' variable would need to be set to:

 

'(2 2 2 2 2)

 

rather than the same as 'lst1'.

Kent Cooper, AIA
Message 14 of 15

Kent1Cooper
Consultant
Consultant

[It seems to me that Post 2 is really the Solution to the original question in Post 1, and that Post 8 is not, but (however helpful it may be) is actually an answer to the different question in Post 3.  But I'll suggest that @vishshreevT578L change the designations if that's correct, rather than do it myself.]

Kent Cooper, AIA
0 Likes
Message 15 of 15

patrick_35
Collaborator
Collaborator

Hi

 

You are right to take me back and simplify the function because I did not think


The idea is just to show the addition of two lists, regardless of the numbers. Otherwise, I agree with what was previously proposed.

 

@+

0 Likes