[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: no empty (zero) string predicate in Elisp

From: Pascal J. Bourguignon
Subject: Re: no empty (zero) string predicate in Elisp
Date: Sat, 25 Apr 2015 16:48:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Stefan Monnier <address@hidden> writes:
>         Stefan "yes, I know Elisp has `zerop' but that's stupid as well"

It's arguable.  Cf. the discussion abouto 1+ and 1-.

Both for fundamental reasons, cardinals are built from a zero and a
successor relationship, therefore a predicate for zero is not stupid,
and a 1+ function neither;  and for optimization reasons on simplistic
compilers: the hardware usually HAS specific (and optimized)
instructions to test for zero and another to increment.

On the other hand, from the "highlevel" point of view, we may choose to
provide a language with only generalized operators.

And I'm asking, why stop at equal or string=?  
Why not define a equivalence class membership predicate?

(equiv-class-member-p 'abc  (string-equivalence-class "abc")) -> t
(equiv-class-member-p "123" (number-equivalence-class 123.0)) -> t
(equiv-class-member-p "" (sequence-length-class '())) --> t

So yes, if you don't want to have (zerop (length "")), then do not stop
at (= 0 (length "")), write: 
(equiv-class-member-p "" (sequence-length-class '()))

Since testing for empty sequences is something that is natural and as
often done as testing for zero, having an emptyp predicate for sequences
seems very natural, so defining:

    (defun emptyp (sequence)
      (zerop (length sequence)))

seems good:

   (list (emptyp '())
         (emptyp [])
         (emptyp "")) --> (t t t)

In the case of the emacs lisp byte compiler, it looks like using (= 0 x)
instead of (zerop x) would be more efficient, because the compiler in
emacs-version "24.3.1" seems to generate a more efficient byte code:

    (disassemble (byte-compile (defun emptyp (sequence)
                                 (= 0 (length sequence)))))
        byte code:
          args: (sequence)
        0       varref    sequence
        1       length    
        2       constant  0
        3       eqlsign   
        4       return    

    (disassemble (byte-compile (defun emptyp (sequence)
                                 (zerop (length sequence)))))
        byte code:
          args: (sequence)
        0       constant  zerop
        1       varref    sequence
        2       length    
        3       call      1
        4       return    

On the other hand, eqlsign must perform two type checks and has to
convert 0 to float when the value is float (or would have to convert a
0.0 to int when the value is int), so it's not obvious which would be
more efficient in the current implementation.

__Pascal Bourguignon__       
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

reply via email to

[Prev in Thread] Current Thread [Next in Thread]