Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:27:37+00:00 2026-06-13T22:27:37+00:00

I am trying to define a recursive function balanced that takes a string and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:27:39+00:00Added an answer on June 13, 2026 at 10:27 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a method in my vb.net interface that will only accept
In Haskell I can easily define a recursive function which takes a value and
I am trying to make a function that takes 2 numbers and will create
I'm trying to use Function to define a recursive definition using a measure, and
I'm stuck on a homework question. I'm trying to define a function that checks
I'm trying to define a function that will allow me to get a value
I am trying to define an abstract class that has operators to compare two
I'm trying to define a very simple function in Drools as below: import java.util.List;
I'm trying to define a service endpoint in my web.config file so that I
im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.