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 8493107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:54:57+00:00 2026-06-10T22:54:57+00:00

If we define a function something like (defun foo(x) (setf x somevalue)) Is x

  • 0

If we define a function something like

(defun foo(x)
  (setf x somevalue))

Is x defined as a local variable or global? using setf/q is setting the value to be global.
if it is global can anyone tell me how to define a local variable in lisp other than let?

Thanks!

Consider the following example

(let ((x 0)) 
  (defun foo (y) 
    (when (equal x 0) (setq x y)) 
    (when (< x y) (setq x y))
    x))

when I am giving some input to foo like (foo 2) , it is returning 2 and if we execute the function again with (foo 1) it still returns 2 and (foo 3) returns 3.This is what i actually want it do.But the question is how is this possible, because if I try to access the variable x outside the function from the clisp terminal, I am unable to.If I am accessing the function again, it seems to retain the previous value of x.

thank you!

  • 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-10T22:54:59+00:00Added an answer on June 10, 2026 at 10:54 pm

    To draw a parallel to other languages like C, C++, Java or Python what your code changes is a “local variable” even if this is not a wording that a Lisper would use (the wording in Lisp parlance would be a local “binding”).

    You can create local variables by using function parameters like your example does, or using some standard forms like:

    • (let ((x 12)) ...)
    • (do ((x 0 (1+ i))) ...)
    • (dotimes (x 10) ...)
    • (loop for x from 0 to 10 do ...)

    On the other hand it is possible that in your implementation all local variables are created using parameters and other forms simply are macros expanding to that. For example:

    (let ((x 10)) ...)
    

    is equivalent to

    (funcall (lambda (x) ...) 10)
    

    Note also that indeed reading your code fragment it’s possible that x is in a sense a “global variable” because it could have been declared special:

    (defvar x 12)
    ;; ==> x
    
    (defun bar ()
      (format t "The value of x is ~a" x))
    ;; ==> bar
    
    (defun foo (x)
      (bar))
    ;; ==> foo
    
    (foo 42)
    The value of x is 42
    ;; ==> NIL
    
    x
    ;; ==> 12
    

    If you declare a variable “special” using (defvar ...) it will be handled differently: it’s like every time you use it as a parameter or you use it in a (let ..) form what the code will do is saving the current value, using the new provided value and then restoring the value after you exit the function or let.

    So those variables are both “global” (because outer functions can see them) but also local (because after your function or let terminates the previous value will be restored).

    The standard convention is to name special variables with “earmuffs” i.e. with an asterisk both at the start and at the end of the name like:

    (defvar *x* 12)
    

    This helps who reads your code to understand that the variable is special. Note that however this is not mandated by the language and any name can be used for a special variable.

    There is nothing similar to special variables in C, C++, Java or Python.

    One last note about setq and setf. Things are a bit tricky here because you need to understand lower levels of Lisp to see why setq is needed. If you are using Common Lisp then you should simply forget about setq and always use setf.

    setf is a macro that will expand to setq when needed (however also setq can change into setf when needed (symbol macros) and this is where things may get confusing for a newbie).

    Your last example is a case of a “closure”. When you define a function (either named or unnamed with a (lambda ...) form) the function can “capture” the variables that are visible and use them later. A simpler case often shown is the “adder”:

    (defun adder (x)
      (lambda (y) (incf x y)))
    

    this function returns a function that will keep adding the passed value to an internal totalizer:

    (let ((f (adder 10)))
      (print (funcall f 3))
      (print (funcall f 9))
      (print (funcall f 11)))
    

    the output will be 13 (10 + 3), 22 (13 + 9) and 33 (22 + 11).

    The anonymous function “captured” the local variable x and can use it even after exiting the adder function. In languages like C, C++ or Java a local variable cannot survive when you exit the scope that defined the variable.

    C++11 has unnamed functions, but still variables cannot be captured and survive the scope (they can be copied to local variables of the unnamed function, but this is not the same thing).

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

Sidebar

Related Questions

I have grid something like this: Ext.define('Exp.view.dashboard.Tv', { extend: 'Ext.grid.Panel', initComponent: function() { this.columns
I am loading some template files into my application using require.js like so: define(function(require)
Suppose I have a function (foo) defined as (defun foo () (read-from-minibuffer What? ))
Is it possible to define the WordPress Permalink in functions.php using something like this:
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
Is it possible to define a function and use without ()? Like require(a.php) and
The usual definition for a specialization of a template function is something like this:
I would like to define a nullary static template member function which would be
As an exercise I am trying to define let as a lambda function something

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.