2
$\begingroup$

I would like to understand why in the following code, my variable Λ is recognized by the system but not n.

In fact for me Λ shouldn't be recognized as it is defined in a function.

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ What does Context["n"] return? $\endgroup$
    – Carl Woll
    Jun 6, 2017 at 16:01
  • $\begingroup$ Or, what does Block[{$ContextPath = {"System"}, $Context = "System"}, RawBoxes@ToBoxes@DownValues[qDef]] return? $\endgroup$
    – Carl Woll
    Jun 6, 2017 at 16:20
  • $\begingroup$ Works as expected on my Mac with v11.1.1 Try starting with a fresh kernel. $\endgroup$
    – Bob Hanlon
    Jun 6, 2017 at 17:01
  • $\begingroup$ maybe the first two lines not shown are something like local`n ; $ContextPath = Prepend[$ContextPath, "local`"] . $\endgroup$
    – george2079
    Jun 6, 2017 at 18:09
  • 1
    $\begingroup$ Posting actual code is better than posting a picture of code. $\endgroup$ Jun 6, 2017 at 22:44

1 Answer 1

3
$\begingroup$
  • I would like to understand why in the following code, my variable "Lambda" is recognized by the system but not "n".

    Not quite correct, you just showed n isn't in Global` context. When could it happen? That is, when could n not be in Global` and still be displayed as n?

    Whenever n is on $ContextPath or $Context. Those values affect typesetting, see:

    FullForm with context for each symbol?

    and check:

    PacletManager`foo;    (*PacletManaget` is loaded on startup so it is on $ContextPath*) 
    foo = 1;    
    Context @ foo
    
    "PacletManager`"
    

    You need to read:

    How symbol lookup actually works

  • in fact for me Lambda shouldn't be recognized as it is defined in a function.

    It is not defined, it is mentioned, symbols are created just by parsing input.

    Quite often common names like n, x, i, k, foo, bar will exists in $ContextPath but that won't prevent them from being scoped in different functions as it is a task for scoping constructs, your function takes care of that.

    tutorial / VariablesInPureFunctionsAndRules

    What are the use cases for different scoping constructs?


Further reading:

Context of localised (dynamic) symbols

Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Where does a package have to be loaded?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.