Question regarding a mapcar lambda code

Question regarding a mapcar lambda code

Anonymous
Not applicable
2,299 Views
23 Replies
Message 1 of 24

Question regarding a mapcar lambda code

Anonymous
Not applicable

Hi all!

 

I have a list of a lists called in this example "list"

 

It looks like this:

((1.0 1.0 0.0) (3.0 5.0 0.0) (6.0 4.0 0.0))

 

I would like to add 5.0 to the car of each list, 4.0 to the cadr of each one, and nothing to the caadr of them. (i can even omit the third part)

 

So i would like to get this at the end in a variable called offs.

((6.0 5.0 0.0) (8.0 9.0 0.0) (11.0 8.0 0.0))

 

I am trying this line:

 

(setq offs (mapcar '(lambda (a) ((+ 5.0 (car a))(+ 4.0 (cadr a))(+ 0 (caadr a)))) list))

 

But of course it doesnt work, and i feel like its total wrong, because i dont know what will mapcar return in this case, but i dont think its even a list. I think i should include "list" and "append" too to somewhere.

 

Can someone help? 🙂

 

Thanks in advance!

0 Likes
Accepted solutions (4)
2,300 Views
23 Replies
Replies (23)
Message 21 of 24

hak_vz
Advisor
Advisor

 

(or *n (setq *n 3)

 

 

Show this to inexperienced autolisp programmer and he'll wonder what this asterisk before n means, is this multiplication, how it combines with boolean OR..... You have all elaborated various cases and they are all legal.

In other languages i.e. javascript there are guides for writing readable code , for example D. Crockford's The elements of Javascript style. It's a different language, but some advice are applicable.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 22 of 24

ВeekeeCZ
Consultant
Consultant

Here are some simple examples of while to think about. It's all about the testexpr which decides whether it stops (is nil) or goes to the next round. The expr ... does not really matter - you don't need it.

 

(while testexpr
  [expr ...])


(while (setq pt (getpoint "Specify next point or : "))
  (do something with pt))


(while (progn
	 (initget 2)
	 (setq pt (getpoint "Specify next point or : ")))
  (do something with pt))


(while (and (not (initget 2))
	    (setq pt (getpoint "Add next point or : "))
	    (setq lst (cons (distance pt p0) lst))))


(setq done nil)
(while (not done)
  (initget "Exit")
  (setq pt (getpoint "Specify next point or [Exit]: "))
  (cond ((= pt "Exit")
	 (setq done T))
	(T
	 (do something...))))

 

Message 23 of 24

ВeekeeCZ
Consultant
Consultant

@ВeekeeCZ wrote:

Here are some simple examples of while to think about. It's all about the testexpr which decides whether it stops (is nil) or goes to the next round. The expr ... does not really matter - you don't need it.

 

(while testexpr
  [expr ...])


(while (setq pt (getpoint "Specify next point or <exit>: "))
  (do something with pt))


(while (progn
	 (initget 2)
	 (setq pt (getpoint "Specify next point or <exit>: ")))
  (do something with pt))


(while (and (not (initget 2))
	    (setq pt (getpoint "Add next point or <exit>: "))
	    (setq lst (cons (distance pt p0) lst))))


(setq done nil)
(while (not done)
  (initget "Exit")
  (setq pt (getpoint "Specify next point or [Exit]: "))
  (cond ((= pt "Exit")
	 (setq done T))
	(T
	 (do something...))))

 


Now came a notification and I see, that the side messed the code up - it removed <exit>. That's important to understand...

0 Likes
Message 24 of 24

ronjonp
Advisor
Advisor

@ВeekeeCZ  FWIW

 

 

(while (and (not (initget 2)) (setq pt (getpoint "Add next point or : ")) (setq lst (cons pt lst))))
;; Another way ( back to the (progn) question )
(while (progn (initget 2) (setq pt (getpoint "Add next point or : "))) (setq lst (cons pt lst)))

 

 

And another example to append distances and draw vectors:

 

 

(defun c:mdist (/ p1 p2 d v)
  (setq d 0)
  (while (and (or p1 (setq p1 (getpoint "\nSpecify start point: ")))
	      (setq p2 (getpoint p1 "\nSpecify next point: "))
	 )
    (setq d  (+ d (distance p1 p2))
	  v  (append v (list p1 p2))
	  p1 p2
    )
    (grvecs (append v '(1)))
    (princ (strcat "\nRunning total: " (rtos d)))
  )
  (princ (strcat "\nTotal distance: " (rtos d)))
  (princ)
)

 

 

0 Likes