[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: to big nest level of recursion
From: |
Pascal Bourguignon |
Subject: |
Re: to big nest level of recursion |
Date: |
Mon, 20 Mar 2006 14:52:43 +0100 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
"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.