lfe011969 Posted June 22, 2012 Posted June 22, 2012 My job took me away from AutoCAD for a good while but I'm back behind a desk and I'm really rusty with my AutoLISP. How can I take a list of numbers and put them of groups of two? For example, if my list is this: (1 2 3 4 5 6 7 How can I get it to look like this: (1 2)(3 4)(5 6)(7 or even this: (1 . 2)(3 . 4)(5 . 6)(7 . As always, any help would be appreciated. Quote
Lee Mac Posted June 22, 2012 Posted June 22, 2012 (defun f ( l ) (if l (cons (list (car l) (cadr l)) (f (cddr l))))) (defun g ( l ) (if l (cons (cons (car l) (cadr l)) (g (cddr l))))) Generic function. Quote
lfe011969 Posted June 22, 2012 Author Posted June 22, 2012 I guess I should've known you would already have a solution. Thanks! Quote
David Bethel Posted June 22, 2012 Posted June 22, 2012 Or maybe: (defun test (l / tmp) (and (zerop (rem (length l) 2) (while (< (length l) 1) (setq tmp (cons (list (car l) (cadr l)) tmp) (setq l (cddr l)) tmp)) -David Quote
pBe Posted June 22, 2012 Posted June 22, 2012 Or maybe: (defun test (l / tmp).... -David something missing David? .. perhaps (defun test (l / tmp) (and (zerop (rem (length l) 2)) (while (> (length l) 1) (setq tmp (cons (list (car l) (cadr l)) tmp) l (cddr l)) )) (reverse tmp) ) (defun _relist (l n / x ln ls) (if (zerop (rem (length l) n)) (repeat (/ (setq ln (length l)) n) ((lambda (j) (repeat j (setq x (cons (nth (setq ln (1- ln)) l) x) ))) n) (setq ls (cons x ls) x nil) )) ls) (_relist '(1 2 3 4 5 6 7 8 9 10) 1) ((1) (2) (3) (4) (5) (6) (7) (8 ) (9) (10)) (_relist '(1 2 3 4 5 6 7 8 9 10) 2) ((1 2) (3 4) (5 6) (7 8 ) (9 10)) (_relist '(1 2 3 4 5 6 7 8 9 10) 3) nil (_relist '(1 2 3 4 5 6 7 8 9 10) 4) nil (_relist '(1 2 3 4 5 6 7 8 9 10) 5) ((1 2 3 4 5) (6 7 8 9 10)) 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.