marko_ribar 发表于 2022-7-6 08:02:52

 
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))

Lee Mac 发表于 2022-7-6 08:05:25

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)))

robierzo 发表于 2022-7-6 08:10:05

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.

robierzo 发表于 2022-7-6 08:12:51

Now I can see them.

Lee Mac 发表于 2022-7-6 08:14:40

 
This is because you have used "...." in your thread title.

marko_ribar 发表于 2022-7-6 08:18:20

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))

Lee Mac 发表于 2022-7-6 08:22:22

 
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.

robierzo 发表于 2022-7-6 08:25:44

O.K. making way....Thanks

marko_ribar 发表于 2022-7-6 08:28:55

;; 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.

Lee Mac 发表于 2022-7-6 08:30:36

 
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]
查看完整版本: convert list with mapcar, lamb