From while to fold (in F#)
richardminerich.com
richardminerich.com
(defun counter (numbers)
(reverse
(reduce #'(lambda (acc element)
(cond ((null acc) (list element 1))
((= (first acc) element)
(cons element (cons (1+ (second acc)) (cddr acc))))
(t (cons element (cons 1 acc)))))
numbers
:initial-value nil)))
(counter '(4 4 5 5 5 3 3))