(defun count (lst / i cnt nlst)
(while (setq i (car lst))
(setq lst (cdr lst)
cnt (length lst))
(setq lst (vl-remove i lst)
nlst (cons (list i (1+ (- cnt (length lst)))) nlst))
)
nlst
)
(defun count2 (In_Lst / OutLst TmpLst)
(while In_Lst
(setq
TmpLst (car In_Lst)
OutLst
(cons
(cons
TmpLst
(-
(length In_Lst)
(length (setq In_Lst (vl-remove TmpLst In_Lst)))
)
)
OutLst
)
)
)
)
; Michael Puckett - Foreach "old style"
(defun TallyList ( lst / result pair )
(reverse
(foreach x lst
(setq result
(if (setq pair (assoc x result))
(subst (cons x (1+ (cdr pair))) pair result)
(cons (cons x 1) result)
)
)
)
)
)
(defun _count ( l / c x )
(if (setq x (car l))
(progn
(setq c (length l)
l (vl-remove x (cdr l))
)
(cons (cons x (- c (length l))) (_count l))
)
)
)
(defun countOLDer (lst / counted)
(setq lst (acad_strlsort lst))
(while (setq a (car lst))
(setq n 1)
(while (eq a (car (setq lst (cdr lst))))
(setq n ( + n 1))
)
(setq counted (cons (list a n) counted)))
)
Code: [Select](defun countOLDer (lst / counted)
< ... >
Another:Code: [Select](defun _count ( l / c x ) ...
Another:Recursion is often shorter and beautiful but sometimes can be slower and can bomb on very very very log list:Code: [Select](defun _count ( l / c x ) ...
Code: [Select](defun countOLDer (lst / counted)
< ... >
Nice variation pBe :)
(defun countvar (lst / a b c d n)
(while (setq a (Car lst))
(setq n 0)
(while (setq b (assoc (Car a) lst))
(setq n (1+ n )
c (cons (cadr b) c)
lst (vl-remove b lst)))
(setq d (cons (list (Car a) n c) d)
c nil))
d
)
One more to boil the brain :lol:
Very deep but slower :-D
(defun _count (l / nl)
(foreach item l
(if (not(member item nl))
(setq
nl (append
nl
(list item
(- (length l) (length (setq l (vl-remove item l))))
)
)
)
)
)
nl
)
It is a very new approach :kewl:Very deep but slower :-D
If I were aiming for raw performance, I would not be using recursion... :roll:
It is a very new approach :kewl:
:-D
It is a very new approach :kewl:
:-D
I guess you're new here :lol:
new but very old... :cry:It is a very new approach :kewl:
:-D
I guess you're new here :lol:
regardless, recursion does work and is elegant and useful in some cases ... but you knew that already ;-)Yes about recursion.
I think you can shave a little more off your function Lee (forgive me if I'm wrong; I have a hard time with lisp now).
Quick once through look.Code - Auto/Visual Lisp: [Select]
Not quite ...
(defun countvar (l / a b c d )
(while (setq a (Car l))
(while (setq b (assoc (Car a) l))
(setq c (cons (cadr b) c)
l (vl-remove b l)))
(setq d (cons (list (Car a) c) d)
c nil))
d
)
The [much faster] iterative version might be:Yes it is very similar but is faster on long list:Code - Auto/Visual Lisp: [Select]
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 32768 iteration(s):
(COUNT2 ALIST)......1966 / 1.03 <fastest>
(_COUNT3 ALIST).....2028 / 1 <slowest>
Elapsed milliseconds / relative speed for 1 iteration(s):
(_COUNT3 ALIST).....2996 / 1.26 <fastest>
(COUNT2 ALIST)......3776 / 1 <slowest>
(defun _Positions ( x lst / p )
;; find all the n positions of x in lst
;; (_Positions 1 '(0 0 1 0 0 1)) >> (2 5)
(if (setq p (vl-position x lst))
( (lambda ( lst result )
(while (setq p (vl-position x lst))
(setq
result (cons (+ 1 p (car result)) result)
lst (cdr (member x lst))
)
)
(reverse result)
)
(cdr (member x lst))
(list p)
)
)
)
(defun _Tally ( x lst )
;; count all the occurances of x in lst
;; (_Tally 1 '(0 0 1 0 0 1)) >> 2
(length (_Positions x lst))
)
(progn
(setq aList nil Countr 0)
(repeat 10000 (setq aList (cons (setq Countr (1+ Countr)) aList)));100000
(repeat 4 (setq Alist (append Alist Alist))) ;5
(princ "\nLength : ") (princ (length aList))
(princ " Positions: ") (princ (length (ALE_PositionS 1 Alist))) (princ "\n ") (princ)
)
(Benchmark '(
(MP_Positions 1 Alist)
(SK_index 1 Alist)
(MP_Positions 1 Alist)
(SK_index 1 Alist)
)
Length : 160000 Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
(SK_INDEX 1 ALIST).........1531 / 1.01 <fastest>
(MP_POSITIONS 1 ALIST).....1546 / 1
(SK_INDEX 1 ALIST).........1547 / 1
(MP_POSITIONS 1 ALIST).....1547 / 1 <slowest>
Length : 160000 Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
(MP_POSITIONS 1 ALIST).....1547 / 1 <fastest>
(SK_INDEX 1 ALIST).........1547 / 1
(MP_POSITIONS 1 ALIST).....1547 / 1
(SK_INDEX 1 ALIST).........1547 / 1 <slowest>
; Stefan
(defun SK_index (n l / i j r)
(setq j -1)
(while (setq i (vl-position n l))
(setq r (cons (setq j (+ 1 i j)) r))
(setq l (cdr (member n l)))
)
(reverse r)
)
Of all the weird ways to say "that flavor has already been written" that was the most recent I've read.https://www.theswamp.org/index.php?topic=50539.msg556727#msg556727
I don't recall seeing Stefan's submission before -- great minds think alike?
(list
'("A" "B" 10)
'("A" "C" 20)
'("B" "C" 10)
'("A" "B" 10)
'("A" "C" 20)
'("A" "B" 10)
)
-->
(list
'(("A" "B" 10) . 3)
'(("A" "C" 20) . 2)
'(("B" "C" 10) . 1)
)
It's been done. I'd search for you but I'm sick as a dawg right now.Thanks for comfirming, I feel relatively new for theswamp so I have no idea whats below the surface, expect big fishes.
(defun _Positions ( x lst / p )
;; find all the positions of x in lst
;; (_Positions 1 '(0 0 1 0 0 1)) >> (2 5)
(if (setq p (vl-position x lst))
( (lambda ( lst result )
(while (setq p (vl-position x lst))
(setq
result (cons (+ 1 p (car result)) result)
lst (cdr (member x lst))
)
)
(reverse result)
)
(cdr (member x lst))
(list p)
)
)
)
(defun _Tally ( x lst )
;; count all the occurances of x in lst
;; (_Tally 1 '(0 0 1 0 0 1)) >> 2
(length (_Positions x lst))
)
(defun _Distil ( lst / result )
(while lst
(setq
result (cons (car lst) result)
lst (vl-remove (car result) (cdr lst))
)
)
(reverse result)
)
(defun _TallyHo ( lst )
(mapcar
(function (lambda (x) (cons x (_Tally x lst))))
(_Distil lst)
)
)
Cool beans, thanks Marc.If we can go on demential:
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Length : 160000 Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
(ALE_POSITIONSD 1 ALIST).....1828 / 2.56 <fastest>
(ALE_POSITIONSD 1 ALIST).....1844 / 2.53
(MP_POSITIONS 1 ALIST).......4625 / 1.01
(MP_POSITIONS 1 ALIST).......4672 / 1 <slowest>
; 20161206
(defun ALE_PositionSd (i l / n r c) ; ALE_PositionS(demential)
(setq n -1)
(while (setq c (vl-position i l))
(setq l (cdr (ALE_List_NthCdr c l)) r (cons (setq n (+ 1 c n)) r))
)
(reverse r)
)
ALE_List_NthCdr see: https://www.theswamp.org/index.php?topic=46419.msg514475#msg514475Given a couple quick and dirty functions (not hardly optimized) ...This is a good start, thanks alot MP! :yay!:
<...>
Cheers (now where'd I'd leave that big bowl ...).