From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: to big nest level of recursion Date: Mon, 20 Mar 2006 14:52:43 +0100 Organization: Informatimago Message-ID: <871wwx6x5g.fsf@thalassa.informatimago.com> References: <1142858108.336902.127520@j33g2000cwa.googlegroups.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1142865639 28095 80.91.229.2 (20 Mar 2006 14:40:39 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 20 Mar 2006 14:40:39 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 20 15:40:35 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1FLLYY-0002b2-Td for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Mar 2006 15:40:27 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1FLLYY-0005Yk-Bh for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Mar 2006 09:40:26 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 57 Original-X-Trace: individual.net 75RS/vwBL3b94mm54DVPGwjmbVKBBAEbVjbB4VUbgDrkKiHZGe Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) Cancel-Lock: sha1:Nrrhko2UYKV/h/teAQ74mW2fMTM= Original-Xref: shelby.stanford.edu gnu.emacs.help:138283 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:33895 Archived-At: "Anton V. Belyaev" writes: > Hi! I wrote a simple function which fails on lists long enougth with > reason "(error "Lisp nesting exceeds `max-lisp-eval-depth'")". But the > function contains right recursion. Does Emacs LISP interpreter unwind > right recursion calls? > > (defun contains (lst el) > (if (null lst) > nil > (if (eq (car lst) el) > t > (contains (cdr lst) el) > ) > ) > ) Let's make it readable: 1- indentation and parenthesis placement: (defun contains (lst el) (if (null lst) nil (if (eq (car lst) el) t (contains (cdr lst) el)))) 2- use of cond in place of if sequences: (defun contains (lst el) (cond ((null lst) nil) ((eq (car lst) el) t)) (t (contains (cdr lst) el))) 3- since all the branches return a boolean anyway, we can write it as a boolean expression directly: (defun contains (lst el) (and (not (null lst)) (or (eq (car lst) el) (contains (cdr lst) el)))) Otherwise, emacs doesn't do TCO. > PS: Does Emacs built-in functions have an equivalent of my contains? member -- __Pascal Bourguignon__ http://www.informatimago.com/ HANDLE WITH EXTREME CARE: This product contains minute electrically charged particles moving at velocities in excess of five hundred million miles per hour.