You're right as always Lee... So my first code :
(defun every2nd ( lst / x ) (setq x -1) (vl-remove nil (mapcar '(lambda ( a ) (if (eq (rem (setq x (1+ x)) 2) 0) a)) lst)))(defun groupby2 ( lst ) (mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst))))(groupby2 '(1 2 3 1 2 3)) No need for the lambda here :
(mapcar '(lambda ( a b ) (list a b)) (every2nd lst) (every2nd (cdr lst)))
(mapcar 'list (every2nd lst) (every2nd (cdr lst))) I can not see post # 11 and following.
I get this message:
ForbiddenYou don't have permission to access /forum/showthread.php on this server.Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Now I can see them.
This is because you have used "...." in your thread title. So, is this the shortest ?
(defun every2nd ( lst / x ) (setq x -1) (vl-remove-if-not '(lambda ( a ) (eq (rem (setq x (1+ x)) 2) 0)) lst))(defun groupby2 ( lst ) (mapcar 'list (every2nd lst) (every2nd (cdr lst))))(groupby2 '(1 2 3 1 2 3))
If you were to remain using the vl-remove-if-not function (or vl-remove-if function, as per my earlier post), then those arrangements would most likely be the shortest.
Shorter code could obviously be obtained by using alternative methods to generate each list supplied to mapcar:
(defun f ( l ) (if l (cons (car l) (f (cddr l)))))(defun g ( l ) (mapcar 'list (f l) (f (cdr l))))...Or of course by abandoning mapcar as stated and demonstrated in my earlier posts. O.K. making way....Thanks ;; Group by number by M.R. ;;(defun gn ( l n / f g k q )(setq k n q l) (defun f ( l n ) (if (and l (> n 0)) (cons (car l) (f (setq q (setq l (cdr l))) (setq n (1- n)))))) (defun g ( l n ) (if q (cons (f q k) (g q k)))) (g q k))(gn '(11 21 31 12 22 32) 2)
M.R.
Since you are calling the function f recursively, the use of setq (as noted the following expression) is unnecessary, as the values of the symbols l and n are passed as arguments to the f function.
(f (setq q (setq l (cdr l))) (setq n (1- n)))Also, by renaming the parameters of the f function, you could remove the need for the extra 'state' variables k & q, e.g.:
(defun gn ( l n / f g ) (defun f ( a b ) (if (and a (< 0 b)) (cons (car a) (f (setq l (cdr a)) (1- b))) ) ) (defun g ( l n ) (if l (cons (f l n) (g l n)))) (g l n))However, note further that the function g is also superfluous since it is identical to the function in which it is defined:
(defun gn ( l n / f ) (defun f ( a b ) (if (and a (< 0 b)) (cons (car a) (f (setq l (cdr a)) (1- b))) ) ) (if l (cons (f l n) (gn l n))))
页:
1
[2]