Skip to content

Emacs 30.0.93 void-variable errors #10

@emacsomancer

Description

@emacsomancer

Maybe I'm doing something wrong (though I've used this package before in the past without issue), but I seem to be getting void-variable errors with the example in the README:

;; -*- lexical-binding: t; -*-
(require 'tco)

(defun sum (n &optional accum)
  (setq accum (or accum 0))
  (if (zerop n)
      accum
    (sum (1- n) (+ accum n))))

(sum 700) ; 245350
(sum 800) ; Lisp error: (excessive-lisp-nesting 1622)

(defun-tco sum-tco (n &optional accum)
  (setq accum (or accum 0))
  (if (zerop n)
      accum
    (sum (1- n) (+ accum n))))

;; Without TCO, values greater than `max-lisp-eval-depth' (usually
;; 600) would cause stack overflow here:
(sum-tco 700) ; Lisp error: (void-variable accum)

Edit: if I do a describe function on sum-tco I see:

;; Could not find source code, showing raw function object.
#[(&rest outer-fun-args)
  ((setq accum (or accum 0))
   (let*
       ((vnew
         #'(lambda (n &optional accum)
             (if (= 0 n) accum (sum (1- n) (+ accum n)))))
        (old (symbol-function 'trampolined-function)))
     (unwind-protect
         (progn
           (fset 'trampolined-function vnew)
           (let
               ((trampolined-result
                 (apply #'trampolined-function outer-fun-args)))
             (while (eq (car-safe trampolined-result) 'tco-sentinel-symbol)
               (setq trampolined-result (funcall (cdr trampolined-result))))
             trampolined-result))
       (fset 'trampolined-function old))))
  (t)]

;; *Symbol Properties*
;; `function-history`
  (nil
   #[(&rest outer-fun-args)
     ((setq accum (or accum 0))
      (let*
          ((vnew
            #'(lambda (n &optional accum)
                (if (= 0 n) accum (sum (1- n) (+ accum n)))))
           (old (symbol-function 'trampolined-function)))
        (unwind-protect
            (progn
              (fset 'trampolined-function vnew)
              (let
                  ((trampolined-result
                    (apply #'trampolined-function outer-fun-args)))
                (while (eq (car-safe trampolined-result) 'tco-sentinel-symbol)
                  (setq trampolined-result (funcall (cdr trampolined-result))))
                trampolined-result))
          (fset 'trampolined-function old))))
     (t)])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions