lambda

lambda

scott_bolton
Advocate Advocate
801 Views
5 Replies
Message 1 of 6

lambda

scott_bolton
Advocate
Advocate

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

0 Likes
Accepted solutions (1)
802 Views
5 Replies
Replies (5)
Message 2 of 6

pbejse
Mentor
Mentor
Accepted solution
(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
Advocate
Advocate

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

 

S

0 Likes
Message 4 of 6

pbejse
Mentor
Mentor

@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
Consultant
Consultant

@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
Mentor
Mentor

@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