Guest kruuger 发表于 2022-7-6 09:49:59

MAPCAR-LAMBDA problem

hello
i wrote something like this. it reads line from file and split it into 3:

(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

(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

Smirnoff 发表于 2022-7-6 10:25:35

(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 (listno need because mapcar creates list by himself.

Smirnoff 发表于 2022-7-6 10:33:04

And without extra variables:
 

(defun kr:SplitList3 (In) (mapcar'(lambda (%)       (list         (mapcar          '(lambda (%1)             (substr % (car %1) (last %1))         )         (list '(1 10) '(10 16) '(26 5))         )       )   )   In ))
 

Guest kruuger 发表于 2022-7-6 11:01:55

Beauty:) Thanks Smirnoff
like this is perfect:

(defun kr:SplitList3 (In) (mapcar'(lambda (%)   (mapcar      '(lambda (%1)         (substr % (car %1) (last %1))       )       (list '(1 10) '(10 16) '(26 5))   )   )   In ))kruuger
页: [1]
查看完整版本: MAPCAR-LAMBDA problem