Guest kruuger Posted February 5, 2011 Posted February 5, 2011 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 Quote
Smirnoff Posted February 5, 2011 Posted February 5, 2011 (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. Quote
Smirnoff Posted February 5, 2011 Posted February 5, 2011 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 ) ) Quote
Guest kruuger Posted February 5, 2011 Posted February 5, 2011 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.