<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/652851#M84256</link>
    <description>&lt;P&gt;If I'm wrong please correct me, but it would seem that JMP has dynamic-scope with no scope persistence (except for the pseudo-namespaces &lt;CODE class=" language-jsl"&gt;Here&lt;/CODE&gt;, &lt;CODE class=" language-jsl"&gt;Window&lt;/CODE&gt;, &lt;CODE class=" language-jsl"&gt;Box&lt;/CODE&gt;).&amp;nbsp; This contrasts with many popular programming languages that have lexical-scope with full scope-persistence using reference-counters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This caused a lot of head-smashing on my end until I gained the intuition needed to work with the weird scope system that JMP has, and I believe that it's a large point of confusion for many people.&amp;nbsp; Not many programming languages use Dynamic scope (LISP being an important exception, and JSL has some similarities to LISP).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd encourage the JSL documentation team to point out this distinction and provide some resources / links on how to better understand Dynamic scope as it certainly is not the standard way of doing name-resolution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example -- the outer function's local scope is cleared up immediately after the return statement, and the inner functions name-resolution is dynamic (based on the current call-stack):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );

x = 7;
outer function = Function( {},
	{x},
	x = 4;
	inner function = Function( {},
		Print( x )
	);
	Return( Name Expr( inner function ) );
);

func = outer function;
func()

/*** OUTPUT ***
7&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you do something functionally equivalent in a lexically-scoped language with scope-persistence (like Python), then the inner function's enclosing-scope is persistent (it's the local-scope for the outer function), and the name-resolution for the inner function's x-variable is the value 4 from the lexical reading of the system.&amp;nbsp; Since JMP doesn't use lexical-scope, but uses dynamic-scope, then when the inner function is finally executed at the end of the script, it uses dynamic name-resolution of Local -&amp;gt; Here -&amp;gt; Global (and possible column-names for the current table) and finds the value 7.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Understanding this type of behavior is critical for building robust programs in JSL, and I just want to point it out to anyone learning / struggling with JSL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if you have any thoughts / unintuitive examples!&lt;/P&gt;</description>
    <pubDate>Tue, 27 Jun 2023 00:09:04 GMT</pubDate>
    <dc:creator>ErraticAttack</dc:creator>
    <dc:date>2023-06-27T00:09:04Z</dc:date>
    <item>
      <title>JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/652851#M84256</link>
      <description>&lt;P&gt;If I'm wrong please correct me, but it would seem that JMP has dynamic-scope with no scope persistence (except for the pseudo-namespaces &lt;CODE class=" language-jsl"&gt;Here&lt;/CODE&gt;, &lt;CODE class=" language-jsl"&gt;Window&lt;/CODE&gt;, &lt;CODE class=" language-jsl"&gt;Box&lt;/CODE&gt;).&amp;nbsp; This contrasts with many popular programming languages that have lexical-scope with full scope-persistence using reference-counters.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This caused a lot of head-smashing on my end until I gained the intuition needed to work with the weird scope system that JMP has, and I believe that it's a large point of confusion for many people.&amp;nbsp; Not many programming languages use Dynamic scope (LISP being an important exception, and JSL has some similarities to LISP).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd encourage the JSL documentation team to point out this distinction and provide some resources / links on how to better understand Dynamic scope as it certainly is not the standard way of doing name-resolution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example -- the outer function's local scope is cleared up immediately after the return statement, and the inner functions name-resolution is dynamic (based on the current call-stack):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );

x = 7;
outer function = Function( {},
	{x},
	x = 4;
	inner function = Function( {},
		Print( x )
	);
	Return( Name Expr( inner function ) );
);

func = outer function;
func()

/*** OUTPUT ***
7&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you do something functionally equivalent in a lexically-scoped language with scope-persistence (like Python), then the inner function's enclosing-scope is persistent (it's the local-scope for the outer function), and the name-resolution for the inner function's x-variable is the value 4 from the lexical reading of the system.&amp;nbsp; Since JMP doesn't use lexical-scope, but uses dynamic-scope, then when the inner function is finally executed at the end of the script, it uses dynamic name-resolution of Local -&amp;gt; Here -&amp;gt; Global (and possible column-names for the current table) and finds the value 7.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Understanding this type of behavior is critical for building robust programs in JSL, and I just want to point it out to anyone learning / struggling with JSL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if you have any thoughts / unintuitive examples!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2023 00:09:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/652851#M84256</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2023-06-27T00:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/653361#M84280</link>
      <description>&lt;P&gt;This is true, and definitely threw me for a loop when learning the language.&amp;nbsp; The other wrinkle is that JSL is a&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Rewriting" target="_self"&gt;Term Rewriting Language&lt;/A&gt;&amp;nbsp;thanks to its expression objects, which means that while being LISP-ish with respect to scoping, is not LISP-ish with respect to how it handles macros.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only other term-rewriting language I'm aware of in popular use is Wolfram Language.&amp;nbsp;&amp;nbsp;I'm not well versed enough in CS to say for sure, but I have heard some people allege that the term-rewriting paradigm is strictly more powerful than Lisp's macros.&amp;nbsp; On the other hand, I have heard some claim that Wolfram Language is simply an implementation of m-expressions, where Lisp decided s-expressions were good enough.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regardless, JSL's expression system will be foreign to just about everyone, and the ergonomics of their functions for working with expressions don't do them any favors.&amp;nbsp; There is a reason that you can find so many examples in this forum of people using string manipulation and Parse() instead of the term-rewriting features.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2023 18:13:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/653361#M84280</guid>
      <dc:creator>KevW</dc:creator>
      <dc:date>2023-06-27T18:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/653530#M84295</link>
      <description>&lt;P&gt;I'm not a CS person myself, but this stuff on term-rewriting is quite interesting!&amp;nbsp; Thanks for the reference, I'll be up all night on this rabbit-hole now...&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2023 04:46:05 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/653530#M84295</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2023-06-28T04:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/825429#M100561</link>
      <description>&lt;P&gt;The&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;func = outer function;
func();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;puzzled me.&lt;BR /&gt;&lt;BR /&gt;After executing&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;func = outer function();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the definition of inner function is stored in the variable func.&lt;/P&gt;
&lt;P&gt;This is why &lt;FONT face="courier new,courier"&gt;Show(func)&lt;/FONT&gt; returns&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;func = Function({},Print(x));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;At this point, all the information about &lt;FONT face="courier new,courier"&gt;outer function&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;inner function&lt;/FONT&gt; is gone.&lt;BR /&gt;So &lt;FONT face="courier new,courier"&gt;func()&lt;/FONT&gt; will execute&lt;FONT face="courier new,courier"&gt; Print(x)&lt;/FONT&gt; and ... prints &lt;FONT face="courier new,courier"&gt;7&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;edit:&lt;BR /&gt;Actually, there's nothing new compared to what was written in the main post.&lt;BR /&gt;Just the "2D view" of a JSL programmer.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 14:24:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/825429#M100561</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-12-23T14:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/825430#M100562</link>
      <description>&lt;P&gt;JMP keeps variables in the function definition as they are - it doesn't replace them automatically with the current value.&lt;BR /&gt;So, after&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;x = 4;
inner function = Function( {}, Print( x ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;CODE class=" language-jsl"&gt;inner function&lt;/CODE&gt; is defined as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt; Function( {}, Print( x ));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;To make the&lt;FONT face="courier new,courier"&gt; inner function &lt;/FONT&gt;replace it's &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; with the &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; from the &lt;FONT face="courier new,courier"&gt;outer function&lt;/FONT&gt;, one needs some pre-evaluation like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );
x = 7;
outer function = Function( {},
	{x},
	x = 4;
	Eval(Eval Expr( inner function = Function( {},
		x= Expr(x);
		Print( x )
	)));
	Return( Name Expr(inner function));
);

func = outer function;
func()&amp;nbsp;//&amp;nbsp;prints&amp;nbsp;4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;edit: same 2D view with other words.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 14:08:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/825430#M100562</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-12-23T14:08:26Z</dc:date>
    </item>
    <item>
      <title>Re: JMP Scope -- Dynamic vs. Lexical scope and general confusion learning JSL</title>
      <link>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/921116#M107988</link>
      <description>&lt;P&gt;I took a quick look over the fence:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def outer():
    y=4
    def inner():
        print(y)
    return inner

f = outer()
y = 7
f()         # -&amp;gt; 4&lt;/LI-CODE&gt;
&lt;P&gt;So, Python returns 3. Because this is how y is defined in the lexical context when inner is defined.&lt;BR /&gt;"Defined" - or "returned"?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Let's have another look:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def outer():
    y=4
    def inner():
        print(y)
    y=-100
    return inner

f = outer()
y = 7
f()
&lt;/LI-CODE&gt;
&lt;P&gt;-&amp;gt; -100&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 14:54:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/JMP-Scope-Dynamic-vs-Lexical-scope-and-general-confusion/m-p/921116#M107988</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2025-12-23T14:54:57Z</dc:date>
    </item>
  </channel>
</rss>

