From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.devel Subject: Re: lengths and stuff Date: Mon, 28 Dec 2020 08:44:19 -0800 Message-ID: <874kk5ubh8.fsf@ericabrahamsen.net> References: <875z4ob5c9.fsf@gnus.org> <83mtxzly4f.fsf@gnu.org> <87czyvsv3h.fsf_-_@db48x.net> <87v9cnqgjr.fsf@logand.com> <40d9718a-adcc-4ac8-a7dd-8b66270f40d3@default> <87sg7rq8dn.fsf@logand.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2798"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) To: emacs-devel@gnu.org Cancel-Lock: sha1:AzMz/82Ghy0HXp4TFG+GlE8GJbE= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Dec 28 17:45:00 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ktvdj-0000Yx-NH for ged-emacs-devel@m.gmane-mx.org; Mon, 28 Dec 2020 17:44:59 +0100 Original-Received: from localhost ([::1]:37338 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ktvdi-0001WH-PV for ged-emacs-devel@m.gmane-mx.org; Mon, 28 Dec 2020 11:44:58 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40272) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ktvdF-00015V-9U for emacs-devel@gnu.org; Mon, 28 Dec 2020 11:44:29 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]:38804) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ktvdD-0001rl-MC for emacs-devel@gnu.org; Mon, 28 Dec 2020 11:44:29 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1ktvdB-000AIB-Fr for emacs-devel@gnu.org; Mon, 28 Dec 2020 17:44:25 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:262014 Archived-At: Jean Louis writes: > * Tomas Hlavaty [2020-12-27 23:53]: >> On Sun 27 Dec 2020 at 10:52, Drew Adams wrote: >> >> Using length in a predicate is yet completely different and most >> >> likely bad because to answer the predicate, traversing the whole list >> >> is wasted time and energy. >> > >> > (lambda (xs) (= (length xs) 25)) ; need to count elts >> >> Is that a joke? >> >> The predicates were proposed to help programers avoid writing such bad >> code. And if somebody writes such bad code, it is almost trivial to fix >> it with search and replace: >> >> "(= (length" -> "(length=" >> "(< (length" -> "(length<" >> "(> (length" -> "(length>" >> "(/= (length" -> "(length/=" >> "(<= (length" -> "(length<=" >> "(>= (length" -> "(length>=" >> >> Also the "symmetry" (or exhaustiveness?) here is to assist in easily >> fixing bad code with minimum changes. >> >> Nothing of course helps to those who are determined to unneccessarily >> count _all_ the elements of lists. > > May I understand if that proposal is to implement it in C or in Lisp? > Does that mean when (length< list-1 10) is implemented in C it would > become faster than: (< (length list-1) 10) ? > > Or is that proposal to make Lisp functions like: > > (defun length< (list n) > (when (< (length list) n) > t)) They're already implemented in Emacs master, and they're in C. As I understand it, one of the main advantages (beyond being simply faster, possibly) is that they return early: if all you want to do is check if a list has more than 2 elements, you don't have to calculate the whole length, you can return t once you've found the third element.