@quangcongx3
Did you really look into Lee's functions?
;; Round Multiple - Lee Mac
;; Rounds 'n' to the nearest multiple of 'm'
(defun LM:roundm ( n m )
(* m (atoi (rtos (/ n (float m)) 2 0)))
)
(setq s '(3998 3999 4010 3995 4005 4003))
(defun test (l p)
(mapcar
'(lambda (x)
(LM:roundm x p)
)
l
)
)
Result:
Command: (test s 5)
(4000 4000 4010 3995 4005 4005)
Command: (test s 10)
(4000 4000 4010 4000 4010 4000)
Your logic is mathematically flawed. If you round a number like 3995 by a tolerance of 5, it is going to return 3995, not 4000. If you round a number like 3998 by 10, your going to get 4000, not 4010.
As for the grouping part, I am not great at list functions but I used another Lee Mac function to make it work:
;; Count Items - Lee Mac
;; Returns a list of dotted pairs detailing the number of
;; occurrences of each item in a supplied list.
(defun LM:CountItems ( l / c l r x )
(while l
(setq x (car l)
c (length l)
l (vl-remove x (cdr l))
r (cons (cons x (- c (length l))) r)
)
)
(reverse r)
)
;; Groups duplicate items into sublists.
(defun Groupn (l / c r x)
(setq l (LM:CountItems l))
(foreach n l
(if (> (setq c (cdr n)) 1)
(progn
(repeat c (setq x (cons (car n) x)))
(setq r (cons x r) x nil)
)
(setq r (cons (car n) r) x nil)
)
)
(reverse r)
)
Example:
Command: (setq l1 (list 1 1 1 3 3 5 6 2 8 8 8))
(1 1 1 3 3 5 6 2 8 8 8)
Command: (groupn l1)
((1 1 1) (3 3) 5 6 2 (8 8 8))