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

  • SEARCH
  • Home
  • 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 6702921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:06:37+00:00 2026-05-26T07:06:37+00:00

EDIT: I changed the example code after the first answer because I came up

  • 0

EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions.

I am currently learning Common Lisp’s scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below:

Example 1:

(defmethod fun1 (x)
  (print x)
  (fun2))

(defmethod fun2 ()
  (print x))

(fun1 5)

Output:

5 
*** - EVAL: variable X has no value

Question: This makes sense. x is statically scoped and fun2 has no way of finding the value of x without having it passed explicitly.

Example 2:

(defvar x 100)

(defmethod fun1 (x)
  (print x)
  (fun2))

(defmethod fun2 ()
  (print x))

(fun1 5)

Output:

5
5

Question: I don’t understand why x is suddenly visible to fun2 with the value that fun1 gave it, instead of having a value of 100…

Example 3:

(setf x 100)

(defmethod fun1 (x)
  (print x)
  (fun2))

(defmethod fun2 ()
  (print x))

(fun1 5)

Output:

5
100

Question: Should I ignore these results since calling setf on an undeclared variable is apparently undefined? This happens to be what I would expect in my second example…

Any insight would be greatly appreciated…

  • 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-05-26T07:06:37+00:00Added an answer on May 26, 2026 at 7:06 am

    The effects of setting an undefined variable using setf is undefined in ANSI Common Lisp.

    defvar will define a special variable. This declaration is global and also has effect on let bindings. That’s the reason that by convention these variables are written as *foo*. If you have ever defined x with defvar, it is declared special and there is no way to declare it lexical later.

    let by default provides local lexical variables. If the variable was already declared special (for example because of a defvar), then it just creates a new local dynamic binding.

    Update

    • Example 1 .

    Nothing to see.

    • Example 2

    x has been declared special. All uses of the variable x now use dynamic binding.
    When calling the function, you bind x to 5. Dynamically. Other functions can now access this dynamic binding and get that value.

    • Example 3

    This is undefined behavior in Common Lisp. You are setting an undeclared variable. What happens then is implementation dependent. Your implementation (most do something similar) sets the symbol value of x to 100. In fun1, x is lexically bound. In fun2 evaluating x retrieves the symbol value (or possibly to the dynamically bound value) of x.

    As an example for an implementation that did (does?) something else: the CMUCL implementation would also have declare x in example 3 by default to be special. Setting an undefined variable also declares it special.

    NOTE

    In portable standard compliant Common Lisp code the global variables are defined with defvar and defparameter. Both declare these variables to be special. ALL uses of these variables now involve dynamic binding.

    Remember:

    ((lambda (x)
       (sin x))
     10)
    

    is basically the same as

    (let ((x 10))
      (sin x))
    

    Which means that variable bindings in let bindings and variable bindings in function calls are working the same way. If x would have been declared special in some place earlier, both would involve dynamic binding.

    This is specified in the Common Lisp standard. See for example the explanation to the SPECIAL declaration.

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

Sidebar

Related Questions

Edit: From another question I provided an answer that has links to a lot
And why don't they change it? Edit: The reason ask is because I'm new
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
I'm tryring to do a simple insert with foreign key, but it seems that
Edit: I'm rewriting this question because I apparently wasn't clear. Sometimes the GPS service
I found the MSDN example code for getting the default view of a collection
I have an object that is called from two different threads and after it
Many client side code libraries & tool kits, for example Yahoo's YUI and Google's
After enough playing with asynchronous socket programming I noticed that the server was receiving
Just a very general question, that not only applies to this example. Let's say

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.