unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: no empty (zero) string predicate in Elisp
Date: Sat, 25 Apr 2015 16:48:44 +0200	[thread overview]
Message-ID: <874mo4jmb7.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.1591.1429968677.904.help-gnu-emacs@gnu.org

Stefan Monnier <monnier@iro.umontreal.ca> 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__                 http://www.informatimago.com/
“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


  parent reply	other threads:[~2015-04-25 14:48 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-25  3:44 no empty (zero) string predicate in Elisp Emanuel Berg
2015-04-25  9:26 ` Nicolas Petton
2015-05-16  9:23   ` Nicolas Petton
2015-05-16 13:22     ` Bozhidar Batsov
2015-05-16 13:28       ` Bozhidar Batsov
2015-05-16 13:36         ` Bozhidar Batsov
2015-05-16 13:46           ` Eli Zaretskii
2015-05-16 13:43     ` Stefan Monnier
2015-05-16 15:27     ` Drew Adams
     [not found] ` <mailman.1583.1429954907.904.help-gnu-emacs@gnu.org>
2015-04-25 11:22   ` Emanuel Berg
2015-04-25 13:30 ` Stefan Monnier
     [not found] ` <mailman.1591.1429968677.904.help-gnu-emacs@gnu.org>
2015-04-25 14:48   ` Pascal J. Bourguignon [this message]
2015-04-26  2:52     ` Stefan Monnier
2015-04-27 10:28       ` zerop: compiler macro vs defsubst (was: no empty (zero) string predicate in Elisp) Nicolas Richard
2015-04-27 12:19         ` zerop: compiler macro vs defsubst Stefan Monnier
     [not found]     ` <mailman.1626.1430016741.904.help-gnu-emacs@gnu.org>
2015-04-26  4:06       ` no empty (zero) string predicate in Elisp Pascal J. Bourguignon
2015-04-26 21:25         ` Stefan Monnier
2015-04-26  4:26       ` Rusi
2015-04-26 12:36   ` Emanuel Berg
2015-04-26 15:03     ` Pascal J. Bourguignon
2015-04-26 18:34       ` Emanuel Berg
2015-04-26 18:59         ` Marcin Borkowski
     [not found]         ` <mailman.1658.1430074770.904.help-gnu-emacs@gnu.org>
2015-04-26 19:06           ` Emanuel Berg
2015-04-26 20:11             ` Marcin Borkowski
     [not found]             ` <mailman.1662.1430079142.904.help-gnu-emacs@gnu.org>
2015-04-27  1:00               ` Emanuel Berg
2015-04-26 17:28     ` Rusi
2015-04-26 18:39       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874mo4jmb7.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).