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

lambda

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
scott_bolton
603 Views, 5 Replies

lambda

After years of lisping I still haven't got round to using lambda. I have a situation now where I think one would be appropriate. I would normally sort this out with while or foreach or repeat but I'd like to attack this with lambda. I have a list of XYZ points and I would like to create a new list of the difference between the XY and Z of each point with reference to the first point. So:

 

'((10 10 110) (10 34 3) (56 89 245))

 

would result in

 

'((0 0 0) (0 24 -107) (46 79 135))

 

Could someone very kindly point me in the right direction?

 

S

5 REPLIES 5
Message 2 of 6
pbejse
in reply to: scott_bolton

(setq lst '((10 10 110) (10 34 3) (56 89 245)))
(mapcar	'(lambda (l)
	   (mapcar '- l (Car lst))
	 )
	lst
)

HTH

 

Message 3 of 6
scott_bolton
in reply to: pbejse

Jeez, that easy eh? 😉 Thanks, pbejse. Something for me to work on.

 

S

Message 4 of 6
pbejse
in reply to: scott_bolton


@scott_bolton wrote:

Jeez, that easy eh? 😉 Thanks, pbejse. Something for me to work on.


 

You are welcome S,  I'm sure there's another way to code it using lambda, that's the first thing that pops into my head though .

 

Cheers

 

Message 5 of 6
ВeekeeCZ
in reply to: pbejse


@pbejse wrote:
(setq lst '((10 10 110) (10 34 3) (56 89 245)))
(mapcar	'(lambda (l)
	   (mapcar '- l (Car lst))
	 )
	lst
)

HTH

 


 

The above algorithm translated to the common functions for better understanding.

 

(defun :mapcar-alternative (lst-o  / 1st-o idx itm-o itm-n lst-n)
  
  (setq lst-n '()
	1st-o (car lst-o))
  
  (foreach itm-o lst-o
    (setq idx 0
	  itm-n '())
    
    (repeat (min (length itm-o)
		 (length 1st-o))
      (setq itm-n (append itm-n (list (- (nth idx itm-o)
					 (nth idx 1st-o))))
	    idx (1+ idx)))
    
    (setq lst-n (append lst-n (list itm-n))))
  
  lst-n)

 

btw nice one pBe! 👍

Message 6 of 6
CodeDing
in reply to: scott_bolton

@scott_bolton ,

 

This might help you for future reference. It's just a visualization so you can see kind-of what's happening...

 

(defun Add1 (num / ) (+ num 1))
(setq Add2 (lambda (num) (+ num 2)))

(Add1 2) -> returns 3
(Add2 1) -> returns 3

 

Best,

~DD


Need AutoLisp help? Try my custom GPT 'AutoLISP Ace':
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report