乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 85|回复: 3

[编程交流] Arguments function (list of an

[复制链接]

46

主题

92

帖子

45

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
235
发表于 2022-7-5 23:28:35 | 显示全部楼层 |阅读模式
Hi
I have a list of any length
  1. ‘( ‘(a1 b1 c1) (a2 b2 c2) (a3 b3 c3) (a4 b4 c4) …)
I need a arguments function (mapcar) that will give:
 
Example
1.       
  1. (+ a1 a2 a3 a4 …)
2.       
  1. (+ c1 c2 c3 c4 …)
3.       
  1. (+ (* a1 a1) (* a2 a2) (* a3 a3) …)
4.       
  1. (+ (* b1 b1) (* b2 b2) (* b3 b3) …)
5.       
  1. (+ (* a1 b1) (* a2 b2) (* a3 b3) …)
6.       
  1. (+ (* b1 c1) (* b2 c2) (* b3 c3) …)
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 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
 
  1. ; 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)))
 
  1. (setq result (* (* (nth x A)(nth x B))(nth x C))) ; A*B*C
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:08:39 | 显示全部楼层
The traditional use of mapcar for each of the examples would be
  1. (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
回复

使用道具 举报

1

主题

116

帖子

115

银币

初来乍到

Rank: 1

铜币
5
发表于 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
 
  1. (apply 'mapcar (cons '+ lst))
 
which would return
  1. (100 104 108)
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-11 00:44 , Processed in 0.440688 second(s), 60 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表