transcad Posted March 5, 2012 Posted March 5, 2012 I have a list like this (1 2 3 4 5 6 7 8 9 10 11 ...) how can i transform it in this ((1 2 3) (4 5 6) (7 8 9 ) ....) i mean, to group three elements..? Quote
MSasu Posted March 5, 2012 Posted March 5, 2012 Not the most elegant solution: (setq MyNewList '() ;empty lists to build on SubList '()) (foreach item '(1 2 3 4 5 6 7 8 9 10 11) (if (= (length SubList) 3) ;test if set is done (3 items) (setq MyNewList (append MyNewList (list SubList)) ;add done set to main list SubList '()) ;reset ) (setq SubList (append SubList (list item))) ;build set of 3 items ) (setq MyNewList (append MyNewList (list SubList))) ;add last set, may not be complete Regards, Mircea Quote
prakashreddy Posted March 5, 2012 Posted March 5, 2012 Use this one (defun GroupByNum ( l n / a b ) ; l as list & n as number (while l (repeat n (setq a (cons (car l) a) l (cdr l)) ) (setq b (cons (reverse a) b) a nil) ) (reverse b) ) Quote
Stefan BMR Posted March 5, 2012 Posted March 5, 2012 Grouping from list to point list (defun lst3 (lst) (if lst (cons (list (car lst) (cadr lst) (caddr lst)) (lst3 (cdddr lst))) ) ) (lst3 '(1 2 3 4 5 6 7 8 9 10 11 12)) -> ((1 2 3) (4 5 6) (7 8 9) (10 11 12)) (lst3 '(1 2 3 4 5 6 7 8 9 10 11)) -> ((1 2 3) (4 5 6) (7 8 9) (10 11 nil)) Quote
Lee Mac Posted March 5, 2012 Posted March 5, 2012 Here are my solutions: http://lee-mac.com/groupbynum.html Why have you removed the headers from my code prakashreddy? 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.