乐筑天下

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

[编程交流] MAPCAR-LAMBDA problem

[复制链接]

8

主题

159

帖子

153

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 09:49:59 | 显示全部楼层 |阅读模式
hello
i wrote something like this. it reads line from file and split it into 3:
  1. (defun C:TEST (/ FL) (setq FL (getfiled "SelectFile" "*." "txt" ) (princ (kr:SplitList (kr:ReadFile FL))) (princ))(defun kr:ReadFile (File / OFL L LST) (if (findfile File)   (if (setq OFL (open File "r"))     (progn       (setq L "")       (while (/= L :EOF)         (setq LST (cons (setq L (read-line OFL)) LST))       )       (close OFL)       (reverse (cdr LST))     )   )   (alert (strcat "Can't find file " File ".")) ))(defun kr:SplitList (In / LST) (foreach % In   (setq LST     (cons       (list         (substr % 1 10)         (substr % 10 16)         (substr % 26 5)       )       LST     )   ) ) LST)
everything works ok but i want practice mapcar-lambda function so i try to rewrite SplitList function. it is working but returns wrong list. code below
  1. (defun kr:SplitList2 (In / LST 3L) (mapcar  '(lambda (%)     (setq LST       (cons         (mapcar          '(lambda (%1)             (setq 3L (append 3L (list (substr % (car %1) (last %1)))))           )           (list '(1 10) '(10 16) '(26 5))         )         LST       )     )   )   In ) LST)
can someone help with this?
 
thanks
kruuger
回复

使用道具 举报

0

主题

39

帖子

44

银币

限制会员

铜币
-1
发表于 2022-7-6 10:25:35 | 显示全部楼层
  1. (defun kr:SplitList2 (In / LST 3L) (mapcar  '(lambda (%)     (setq LST       (cons         (mapcar          '(lambda (%1)             (setq 3L(substr % (car %1) (last %1)))           )           (list '(1 10) '(10 16) '(26 5))         )         LST       )     )   )   In ) LST)
 
(append 3L (list  no need because mapcar creates list by himself.
回复

使用道具 举报

0

主题

39

帖子

44

银币

限制会员

铜币
-1
发表于 2022-7-6 10:33:04 | 显示全部楼层
And without extra variables:
 
  1. (defun kr:SplitList3 (In) (mapcar  '(lambda (%)       (list         (mapcar          '(lambda (%1)             (substr % (car %1) (last %1))           )           (list '(1 10) '(10 16) '(26 5))         )       )   )   In ))
 
回复

使用道具 举报

8

主题

159

帖子

153

银币

初来乍到

Rank: 1

铜币
40
发表于 2022-7-6 11:01:55 | 显示全部楼层
Beauty:) Thanks Smirnoff
like this is perfect:
  1. (defun kr:SplitList3 (In) (mapcar  '(lambda (%)     (mapcar      '(lambda (%1)         (substr % (car %1) (last %1))       )       (list '(1 10) '(10 16) '(26 5))     )   )   In ))
kruuger
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-7 00:29 , Processed in 0.580109 second(s), 60 queries .

© 2020-2025 乐筑天下

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