Jump to content

Recommended Posts

Posted

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

Posted

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

Posted

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

Posted

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

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...