help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: to big nest level of recursion


From: David Kastrup
Subject: Re: to big nest level of recursion
Date: Mon, 20 Mar 2006 13:51:02 +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)
>       )
>     )
>   )
>
> PS: Does Emacs built-in functions have an equivalent of my contains?

Hardly ever seen something more contorted.  This can be written as

(defun contains (lst el)
  (and lst
       (or (eq (car lst) el)
           (contains (cdr lst) el))))

And without recursion as

(defun contains (lst el)
  (while (and lst (not (eq (car lst) el)))
     (pop lst))
  lst)

And it is available anyway as the basic function "memq", cf
(info "(elisp) Sets and Lists")

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum


reply via email to

[Prev in Thread] Current Thread [Next in Thread]