From: Pascal Bourguignon <usenet@informatimago.com>
Subject: Re: to big nest level of recursion
Date: Mon, 20 Mar 2006 14:52:43 +0100 [thread overview]
Message-ID: <871wwx6x5g.fsf@thalassa.informatimago.com> (raw)
In-Reply-To: 1142858108.336902.127520@j33g2000cwa.googlegroups.com
"Anton V. Belyaev" <anton.belyaev@gmail.com> 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.
next prev parent reply other threads:[~2006-03-20 13:52 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-20 12:35 to big nest level of recursion Anton V. Belyaev
2006-03-20 12:45 ` Hans-Christoph Wirth
2006-03-20 12:51 ` David Kastrup
2006-03-20 13:52 ` Pascal Bourguignon [this message]
2006-03-20 15:56 ` Anton V. Belyaev
2006-03-21 17:09 ` Stefan Monnier
-- strict thread matches above, loose matches on Subject: below --
2006-03-21 21:31 David Reitter
[not found] <mailman.21.1142976703.14011.help-gnu-emacs@gnu.org>
2006-03-25 10:30 ` Alan Mackenzie
2006-03-25 11:46 ` David Kastrup
2006-04-04 13:36 ` david.reitter
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871wwx6x5g.fsf@thalassa.informatimago.com \
--to=usenet@informatimago.com \
/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.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.