Search code examples
emacsrecursionelisp

Recursive function in Emacs Lisp has void-variable error upon evaluating itself


I am trying to define a recursive function balanced that takes a string and returns true only if the parens in the string are balanced.

The elisp code I wrote is based on some Scala code I wrote for the Coursera class by Odersky, it's a transliteration.

  ;; check if expr (list of chars of some expression) is balanced
  (defun balanced (expr)
    (defvar lparen (string-to-char "("))
    (defvar rparen (string-to-char ")"))

    (defun is-open (c) (eq lparen c))
    (defun is-close (c) (eq rparen c))
    (defun is-empty (ls) (eq ls nil))
    (defun is-matching (l r) (and (is-open l) (is-close r)))

    (defun is-balanced (list stack)
      (cond ((is-empty list) (is-empty stack))

      ((is-open (car list))
       (is-balanced (cdr list) ;; 'push' open-paren onto stack
        (cons (car list) stack)))

      ((is-close (car list))

       (if (is-empty stack) nil
         (and 
          (is-balanced (cdr list) (cdr stack))
          (is-matching (car stack) (car list)))))

      (is-balanced (cdr list) (cdr stack))))
  is-balanced

I am in lisp-interaction-mode, so I used Ctrl-J to evaluate the above defun statement.

Then, when I attempt to evaluate this:

  (balanced "(balanced nil nil)")

I get a void-variable error:

Debugger entered--Lisp error: (void-variable is-balanced)
    balanced("(balanced nil nil)")
    (progn (balanced "(balanced nil nil)"))
    eval((progn (balanced "(balanced nil nil)")) t)
    eval-last-sexp-1(t)
    eval-last-sexp(t)
    eval-print-last-sexp()
    call-interactively(eval-print-last-sexp nil nil)
    recursive-edit()
    debug(error (void-variable is-balanced))
    balanced("(balanced nil nil)")
    (progn (balanced "(balanced nil nil)"))
    eval((progn (balanced "(balanced nil nil)")) t)
    eval-last-sexp-1(t)
    eval-last-sexp(t)
    eval-print-last-sexp()
    call-interactively(eval-print-last-sexp nil nil)
    recursive-edit()

The function doesn't seem to recognize itself, what am I doing wrong?


Solution

  • When you execute your function (balanced ...), what it does is this:

    1. It first defines two global variables
    2. It defines five other functions
    3. It accesses the variable is-balanced.

    Step 3. happens right after the end of (defun is-balanced (list stack) ... )))).

    (defun balanced (expr)
        ...
    
        (defun is-balanced (list stack)
          ...
          (is-balanced (cdr list) (cdr stack))))
      is-balanced ;; <= here!
    

    But you have not defined such a variable. You have defined a function of the same name, but you cannot access function objects as if they're variables in Emacs Lisp. See also here. Since there is no variable named is-balanced you get an error message.

    (That said, there's a lot to be critized about the rest of your code. For starters, be aware that by having defuns inside defuns, you're re-defining your is-* functions every single time you're invoking (balanced ...). That's certainly not what you want. Similarly, you probably want to move the defvar outside the function body, since they define global variables. Or did you mean to use (let ...) instead? If you really want global values, then (defconst...) might be more appropriate.)