Path: csiph.com!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 11 Aug 2024 18:56:25 +0000 From: steve g Newsgroups: gnu.emacs.help Subject: Re: TCO with named-let via macros References: <8734oi5ksx.fsf@axel-reichert.de> <87ed6wjffl.fsf@gmail.com> <87y154m645.fsf@axel-reichert.de> Date: Sun, 11 Aug 2024 14:56:19 -0400 Message-ID: <87bk1y51nw.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:1rwbPmVjazFny8dLb8bYo7LxBFQ= MIME-Version: 1.0 Content-Type: text/plain Lines: 76 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-PyUkwHN6X0tmsrnqy5msgsmMagLnLWMaHJ/UaPHIbrWJ/Vx2nbYfie89dz4EN1Orkvll6ex1qcJoWyo!MzmCIHelZ0EFAGrCppe52sZLNlHzfUzJwg/qn2sImuZgsds= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Xref: csiph.com gnu.emacs.help:60996 Axel Reichert writes: > steve g writes: > < > check the elisp manual. it shows how to implement true tail recursion < > in emacs. < > < > (defun elisp-sum (args) < > (elisp-sum-aux args 0)) < > < > (defun elisp-sum-aux (args res) < > (if (null args) < > res < > (elisp-sum-aux (cdr args) (+ (car args) res)))) < > < > you can byte compile it. very simple. > > ... and then easily find out that it will enter the debugger for large > ARGS, say > > (defun elisp-sum (number-sequence 1 10000000)) I was thinking more of the concept; no more than that. (elisp-sum (number-sequence 1 107)).. there is a limit to list length; that is usually time. > But with > > (defmacro defun-tco (name args body) > "Wraps a 'named-let' around BODY to have a TCO of function NAME with ARGS." > (declare (indent defun)) > (let ((bindings (gensym))) > (setq bindings (mapcar #'(lambda (arg) (list arg arg)) args)) > `(defun ,name ,args > (named-let ,name ,bindings > ,body)))) > yes, yes; lambda bindings will certainly work better. even in common lisp there is a limit to the length of &rest arguments and can take some time to process large lists; sometimes crashing the stack. If you are interested in non-deterministic software then screamer is fairly cool. > and > > (defun-tco elisp-sum-aux (args res) > (if (null args) > res > (elisp-sum-aux (cdr args) (+ (car args) res)))) > > a > (defun elisp-sum (number-sequence 1 10000000)) > > will do. this is probably the best option - just mentioning that recursion does work fairly well in emacs lisp... > > Best regards same > Axel