Kowal 发表于 2022-7-5 23:28:35

Arguments function (list of an

Hi
I have a list of any length

‘( ‘(a1 b1 c1) (a2 b2 c2) (a3 b3 c3) (a4 b4 c4) …)
I need a arguments function (mapcar) that will give:
 
Example
1.       
(+ a1 a2 a3 a4 …)
2.       
(+ c1 c2 c3 c4 …)
3.       
(+ (* a1 a1) (* a2 a2) (* a3 a3) …)
4.       
(+ (* b1 b1) (* b2 b2) (* b3 b3) …)
5.       
(+ (* a1 b1) (* a2 b2) (* a3 b3) …)
6.       
(+ (* b1 c1) (* b2 c2) (* b3 c3) …)

BIGAL 发表于 2022-7-6 00:07:00

Ok pretty easy just need to use nth to get a list inside a list with a double repeat Just thought other way is just use below and make 3 new lists A B C then use nth again see second code
 

; example uses strings(setq lst ( list (list "a1" "b1" "c1") (list "a2" "b2" "c2") (list "a3" "b3" "c3") (list "a4" "b4" "c4")))(setq y 0)(repeat (length lst)(setq ans1 (nth y lst)) ; this gives ("a1" "b1" "c1")(setq x 0)(setq ans11 (nth x ans1)) ; gives "a1" (princ ans11) ; just for testing displays to screen(setq x (+ x 1))(setq ans12 (nth x ans1)) ; gives "b1"(setq x (+ x 1))(setq ans13 (nth x ans1)) ; gives "c1" ; do your thing here add together multiple etc(setq y (+ y 1)))
 

(setq result (* (* (nth x A)(nth x B))(nth x C))) ; A*B*C

cwake 发表于 2022-7-6 00:08:39

The traditional use of mapcar for each of the examples would be

(setq lst '((10 11 12) (20 21 22) (30 31 32) (40 41 42)));Example 1(apply '+ (mapcar 'car lst))100;Example 2(apply '+ (mapcar 'caddr lst))108;Example 3(apply '+ (mapcar '(lambda ( x ) (expt (car x) 2)) lst))3000;Example 4(apply '+ (mapcar '(lambda ( x ) (expt (cadr x) 2)) lst))3204;Example 5(apply '+ (mapcar '(lambda ( x ) (* (car x) (cadr x))) lst))3100;Example 6(apply '+ (mapcar '(lambda ( x ) (* (cadr x) (caddr x))) lst))3308

cwake 发表于 2022-7-6 00:47:40

I can't help thinking that the example list is a list of points. If you needed to add up each of the x, y and z values of a list of points it would be more concise to use
 

(apply 'mapcar (cons '+ lst))
 
which would return
(100 104 108)
页: [1]
查看完整版本: Arguments function (list of an