Hi all!
https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scope_vs._dynamic_scope
AddScan =: 3 : 0
add=.4 : 'x + y'
add/\y
)
AddScan 2 3 4
2 5 9
I don't understand how your example shows lexically scoped functions,
but as far as I understand, "add" in the example above is an explicit
definition of a lexically scoped function.
I don't think functions are first class citizens in J according to this
definition. https://en.wikipedia.org/wiki/First-class_citizen
Functions can not return functions. They can take functions as
arguments, but resolution is normally done in some pre-interpretation
stage, like with macros in other languages.
There are ways in explicit code to establish functions from from ascii
representation in runtime. Here is an example:
Scan =: 3 : 0
a=.1!:1 [1
f=. a 5!:0
f/\y
)
Scan 2 3 4
*
2 6 24
Scan 2 3 4
+
2 5 9
This function establishes a function from terminal ascii input.
So, with explicit code you can manipulate ascii representations of
functions and create new functions at what is similar to "runtime". You
can pass these ascii representations as variables. The functions you
create can be used as parameters to functions, as the example shows.
Cheers,
Erling Hellenäs
Den 2017-11-21 kl. 19:56, skrev Alex Shroyer:
@AndrewD: I've been using J casually for about 2 years, and consider myself
an "intermediate beginner". I also use Python, and my impression is that
Python makes it easy to see what the author *wanted* the program to do, but
J makes it easier to see what the program *actually does*.
However, one frustration I still have is regarding explicit definitions.
IMO they should be replaced with something more like what the K language
provides, namely first-class, lexically-scoped functions:
add: {x+y}
scan: {x\y}
scan [add; 2 3 4]
2 5 9
Perhaps J's syntax could be extended someday, to recognize this type of
function in an explicit definition, for example:
add =: dyad def 'x+y'
scan =: HOF def 'x\y' NB. in this scheme, HOF stands for 'higher-order
function' and tells interpreter to not evaluate x or y until both arguments
are bound
add scan 2 3 4
2 5 9
There are a few other things I'd like J to take from K, but that's the big
one.
On Mon, Nov 20, 2017 at 11:16 AM, chris burke <[email protected]> wrote:
Dear All
My attempt to move this thread over to programming failed, but please note
for future use that discussions like this on the language are much better
addressed to the programming forum. Not least, they will then reach all J
forum readers, not just those subscribed to general. See http://code.
jsoftware.com/wiki/System/Forums .
Thanks.
Chris
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm