[Openmcl-devel] (null #())

Raymond Wiker rwiker at gmail.com
Sun Oct 7 09:04:42 PDT 2012


On Oct 7, 2012, at 17:30, Stas Boukarev wrote:

> Raymond Wiker <rwiker at gmail.com> writes:
> 
>> On Oct 7, 2012, at 16:59 , Stas Boukarev <stassats at gmail.com> wrote:
>>> Taoufik Dachraoui <dachraoui.taoufik at gmail.com> writes:
>>> 
>>>> Hi
>>>> 
>>>> how to check that a sequence is null without using length?
>>> (alexandria:sequence-of-length-p sequence 0)
>> 
>> 
>> Or just
>> 
>> (defun is-null-vector (v)
>>              (and (vectorp v)
>>                   (zerop (first (array-dimensions v)))))
> 
> (first (array-dimensions v)) is exactly (length v) on vectors.
> But the question was about sequences, not just vectors. LENGTH on lists
> is O(n), that's why it's undesirable. So, that leaves either
> alexandria:sequence-of-length-p or (or (null x) (zerop (length x)))

The subject of the original email (and all the following ones) indicates that the OP is interested of checking whether a vector has length 0. If the datum is known to be a vector, you may just as well call length on it :-)

If it isn't known to be a vector, the OP (presumably) wants to avoid calling length on a long list, or anything else that might take a long time to evaluate. The code I supplied above will not call #'length on anything, while #'alexandria:sequence-of-length-p will (assuming that my reading of the code is correct.)  (Actually, for lists, #'alexandria:sequence-of-length-p uses nthcdr, which should also be O(n).)




More information about the Openmcl-devel mailing list