3 ms·
It doesn't. COUNT counts when the argument is true. CL:NIL is not true. The correct way to count the number of things, here symbols, is: (loop for s being e
by lispm 7y ago
It doesn't. COUNT counts when the argument is true. CL:NIL is not true.
The correct way to count the number of things, here symbols, is:
(loop for s being each symbol of (find-package :cl) count t)
; -> 978
or use:
(loop for s being each symbol of (find-package :cl) sum 1)
The Common Lisp standard says this in 6.1.3 Value Accumulation Clauses:
> The count construct counts the number of times that the supplied form returns true.
So the original code is NOT equivalent to
(let ((count 0))
(do-symbols (symbol (find-package :cl) count)
(incf count))) ; -> 978
but to
(let ((count 0))
(do-symbols (symbol (find-package :cl) count)
(when symbol ; test if true
(incf count)))) ; -> 977
- ksaj 7y agoThis is correct. It is also stated, albeit in lesser detail, in the comments 14 hours ago, so I'm not sure why it was posted here a half hour ago. SBCL isn't doing it wrong. The programmer is.