User:Devon McCormick/J Functional Meetup Intro
J: A Dynamic Functional Language
J is
What is notation and why should we care?
A good notation aids thought:
by relieving the brain of all unnecessary work, a good notation sets it free
to concentrate on more advanced problems, and in effect increases the mental power of the race.
- - A.N. Whitehead
The quantity of meaning compressed into small space by algebraic signs,
is another circumstance that facilitates the reasonings
we are accustomed to carry on by their aid.
- - Charles Babbage
For example, consider these two statements of the Pythagorean Theorem – one in words, the other in formula and picture:
“Among the three sides of a right triangle (right-angled triangle) the square of the hypotenuse is equal to the sum of the squares of the other two sides”
Passive versus Active Learning
Active is better. For sample problems, take a look at Rosetta Code. The APL/J/K group has been one of the top scorers in this chrestomathy. As an example of the succinctness and power of J, we show this example from Rosetta Code.
Rosetta Code Problem: Zig-zag Matrix
Produce a zig-zag array. A zig-zag array is a square arrangement of the first N2 integers, where the numbers increase sequentially as you zig-zag along the anti-diagonals of the array. For a graphical representation, see JPG zigzag (JPG uses such arrays to encode images).
For example, given 5, produce this array:
0 1 5 6 14 2 4 7 13 15 3 8 12 16 21 9 11 17 20 22 10 18 19 23 24
J Solutions
A succinct way:
($ [: /:@; <@|.`</.@i.)@,~ 5 0 1 5 6 14 2 4 7 13 15 3 8 12 16 21 9 11 17 20 22 10 18 19 23 24
This version is longer, but more "mathematical" and less "procedural":
($ [: /:@; [: <@(A.~_2|#)/. i.)@,~ 5 0 1 5 6 14 2 4 7 13 15 3 8 12 16 21 9 11 17 20 22 10 18 19 23 24
Leveraging a useful relationship among the indices:
($ ([: /:@;@(+/"1 <@|.`</. ]) (#: i.@(*/))))@,~ 5 0 1 5 6 14 2 4 7 13 15 3 8 12 16 21 9 11 17 20 22 10 18 19 23 24
By the way, all the edge cases are handled transparently, without any special checks. Furthermore, by simply removing the trailing @,~ from the solutions, they automatically generalize to rectangular (non-square) matrices:
($ [: /:@; [: <@|.`</. i.) 5 3 0 1 5 2 4 6 3 7 11 8 10 12 9 13 14
Chunking
By grouping concepts and subsuming them into “chunks”, we overcome the limitations of short-term memory. A notation is often an effective way to do this. Another technique for accomplishing this is the development of a specialized vocabulary.
An example of a technical term which encapsulates a lot of meaning into a short phrase is “Hello, World” example. Keep an eye out for this example in the following expositions in which we demonstrate various features of J. We’ll start by learning some specialized J terms.
J Terms: noun, verb, monadic, dyadic, boxed
Noun: basic data on which operations are performed. Nouns types are numeric, character, and boxed. From the J Dictionary:
Nouns are classified in three independent ways: numeric or literal…; open or boxed; arrays of various ranks. The atoms of any array must belong to a single class: numeric, literal… or boxed. Arrays of ranks 0, 1, and 2 are also called atom, list, and table, or, in math, scalar, vector, and matrix.
Some examples of nouns in a J session are as follows (session input is indented three spaces; output is flush against the left margin); “NB.” starts a comment to end of the line. Notice how results are automatically displayed unless assigned or suppressed.
3 NB. Numeric scalar 3 2 3 4 NB. Numeric vector 2 3 4 'A' NB. Character scalar A 'Hello, World' NB. Character vector Hello, World
Monadic: taking one argument (on the right).
Dyadic: taking two arguments (on both sides).
Verb: something which acts on nouns; verbs take nouns as their arguments. A verb may be invoked monadically – with an argument on the right – or dyadically – with an argument on both sides. Some example verbs follow.
1-1 NB. Dyadic invocation of "-" ("minus") 0 -3 NB. Monadic invocation of "-" ("negate") _3 5*1 2 3 NB. Dyadic "*" ("times"): scalar and vector arguments 5 10 15 *_2 _1 0 1 2 NB. Monadic "*" ("signum"): vector argument _1 _1 0 1 1 1 2 3%4 5 6 NB. Dyadic "%" ("divide"): both arguments vectors 0.25 0.4 0.5 %1 2 3 NB. Monadic "%" ("reciprocal"): vector argument 1 0.5 0.33333333