SHARE
TWEET

http://programmingpraxis.com/2012/06/15/counting-ones/

a guest Jun 15th, 2012 57 Never
  1. (defun digits (n &optional (b 10))
  2.   (loop for i = n then (floor i b) with lst = ()
  3.      do (push (mod i b) lst)
  4.      while (> i (1- b))
  5.      finally (return lst)))
  6.  
  7. (defun onep (n) (equal 1 n))
  8. (defun count-ones (n)
  9.   (reduce #'+ (remove-if-not #'onep (digits n))))
RAW Paste Data
Top