Exceed evaluation depth when forward function in Emacs Lisp

64 views Asked by At

Here is just a simplified code snipped I have not managed to work. I do not understand what is wrong.

(defun enumerate-indicies (func)
    (let ((index 0))
        (while (< index 5)
            (funcall func index)
            (setq index (1+ index)))))

(defun enumerate-multiplied-indicies (func)
    (enumerate-indicies #'(lambda (index)
                            (funcall func (* 10 index)))))

The following testing code returns 10 as expected:

(defun test/enumerate-indicies ()
    (let ((sum 0))
          (enumerate-indicies #'(lambda (index)
                                    (setq sum (+ sum index))))
    sum))

(test/enumerate-indicies)

But the below causes error Lisp nesting exceeds max-lisp-eval-depth in my Emacs:

(defun test/enumerate-multiplied-indicies ()
    (let ((sum 0))
          (enumerate-multiplied-indicies #'(lambda (index)
                                             (setq sum (+ sum index))))
    sum))
(test/enumerate-multiplied-indicies)

Could you please give my a clue? That is not supposed be a recursive call here but seems that it is. Thanks.

1

There are 1 answers

3
lawlist On BEST ANSWER

How about using lexical-let to penetrate the lambda? Is the correct answer 100?

(defun enumerate-indicies (func)
  (let ((index 0))
    (while (< index 5)
      (funcall func index)
        (setq index (1+ index)))))

(defun enumerate-multiplied-indicies (func)
  (lexical-let ((func func))
    (enumerate-indicies #'(lambda (index)
      (funcall func (* 10 index))))))

(defun test/enumerate-multiplied-indicies ()
  (lexical-let ((sum 0))
    (enumerate-multiplied-indicies #'(lambda (index)
      (setq sum (+ sum index))))
    sum))

(test/enumerate-multiplied-indicies)