From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: no empty (zero) string predicate in Elisp Date: Sat, 25 Apr 2015 16:48:44 +0200 Organization: Informatimago Message-ID: <874mo4jmb7.fsf@kuiper.lan.informatimago.com> References: <87h9s4rhx5.fsf@debian.uxu> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1429974026 18404 80.91.229.3 (25 Apr 2015 15:00:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 25 Apr 2015 15:00:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Apr 25 17:00:17 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Ym1Z2-0007R5-HJ for geh-help-gnu-emacs@m.gmane.org; Sat, 25 Apr 2015 17:00:16 +0200 Original-Received: from localhost ([::1]:48610 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ym1Z2-0008E3-0b for geh-help-gnu-emacs@m.gmane.org; Sat, 25 Apr 2015 11:00:16 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 82 Original-X-Trace: individual.net FOzkEXyRz5s9Bzv9CrtwDQqZHMruEdc3QJ5Z1yR6x5SJY1LLxi Cancel-Lock: sha1:YTdiNTQ2NThlN2Q1MzE2MTY1OGQwZjlkYWVmODU2ZTRmNjIyNWY0Zg== sha1:HAbrvVLrjMYpEKvRnYTgcFvOhZQ= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:211714 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:103996 Archived-At: Stefan Monnier 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