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
Solved! Go to Solution.
Solved by pbejse. Go to Solution.
(setq lst '((10 10 110) (10 34 3) (56 89 245)))
(mapcar '(lambda (l)
(mapcar '- l (Car lst))
)
lst
)
HTH
@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
@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! 👍
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
Can't find what you're looking for? Ask the community or share your knowledge.