[Jprogramming] k programming challenge
Dan Bron
j at bron.us
Wed Jan 27 16:21:22 UTC 2016
Brian wrote:
> ;:'Though of course, it''s possible to format that result differently.'
> |open quote
> | Though of course, it's possible to format that result differently.
> | ^
> | ;:'Though of course, it''s possible to format that result
> differently.’
So ;: is the J rhematic engine, or lexer. It’s the thing used to chop J sentences into words before they’re put on the stack for the parser.
In other words, it’s the thing used whenever you type a sentence into the J session. And, just as when you type
Though of course, it's possible to format that result differently.
directly into the J session (which is expecting a J sentence), you get:
Though of course, it's possible to format that result differently.
|open quote
| Though of course, it's possible to format that result differently.
|
by the same token (heh!), when you feed
'Though of course, it''s possible to format that result differently.'
to ;: (which is expecting a J sentence), you get:
;: 'Though of course, it''s possible to format that result differently.'
|open quote
| Though of course, it's possible to format that result differently.
| ^
| ;:'Though of course, it''s possible to format that result differently.'
Or, in other words, the monad ;: enforces the rules of J orthography, and one of those rules is every sentence must have an even number of single-quote marks.
I’ve encountered this behavior before but it never struck me as odd, because it’s always been that way. But now that you point it out, it is kind of weird; ;: isn’t just partitioning the sentence the way J would, on top of that it’s validating the sentence.
I suppose if we wanted to tokenize a J sentence but eschew any validation, we could use the dyadic form of ;: with the left argument being the J spelling FSM specified in its vocabulary page [1]:
mj=: 256$0 NB. X other
mj=: 1 (9,a.i.' ')}mj NB. S space and tab
mj=: 2 ((a.i.'Aa')+/i.26)}mj NB. A A-Z a-z excluding N B
mj=: 3 (a.i.'N')}mj NB. N the letter N
mj=: 4 (a.i.'B')}mj NB. B the letter B
mj=: 5 (a.i.'0123456789_')}mj NB. 9 digits and _
mj=: 6 (a.i.'.')}mj NB. D .
mj=: 7 (a.i.':')}mj NB. C :
mj=: 8 (a.i.'''')}mj NB. Q quote
sj=: _2]\"1 }.".;._2 (0 : 0)
' X S A N B 9 D C Q ']0
1 1 0 0 2 1 3 1 2 1 6 1 1 1 1 1 7 1 NB. 0 space
1 2 0 3 2 2 3 2 2 2 6 2 1 0 1 0 7 2 NB. 1 other
1 2 0 3 2 0 2 0 2 0 2 0 1 0 1 0 7 2 NB. 2 alp/num
1 2 0 3 2 0 2 0 4 0 2 0 1 0 1 0 7 2 NB. 3 N
1 2 0 3 2 0 2 0 2 0 2 0 5 0 1 0 7 2 NB. 4 NB
9 0 9 0 9 0 9 0 9 0 9 0 1 0 1 0 9 0 NB. 5 NB.
1 4 0 5 6 0 6 0 6 0 6 0 6 0 1 0 7 4 NB. 6 num
7 0 7 0 7 0 7 0 7 0 7 0 7 0 7 0 8 0 NB. 7 '
1 2 0 3 2 2 3 2 2 2 6 2 1 2 1 2 7 0 NB. 8 ''
9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 NB. 9 comment
)
JFSM =: 0;sj;mj
JFSM ;: 'Though of course, it''s possible to format that result differently.'
+------+--+------+-+--+----------------------------------------------+
|Though|of|course|,|it|'s possible to format that result differently.|
+------+--+------+-+--+----------------------------------------------+
Though of course that’s much less convenient. Also note that we’re still dealing with J’s FSM here, according to which anything inside single-quote marks is a single token, so everything after the apostrophe in “it’s” is run together instead of being broken apart as the preceding words were. In other words, ;: is collecting them to emit them together as a unit, when it “finally” sees the closing single-quote (which, in this case, will never come).
So, if we want to go back to both convenient and closer to the English concept of word formation, we can use ;. or any of its many cover functions (e.g. cut from load’strings’).
Linda wrote:
> Dan, I can see the reasoning behind the fact that the two results
> are like comparing apples and oranges, but to answer the universal
> "true" is jarring, don't you think?
I would find any answer which was *not* 1 (universal true, as you put it) extremely jarring.
Remember that the 1 is coming from -:, not from viewmat in any way. The -: is comparing its inputs, and it does not know, in fact *can not know*, where its inputs are coming from.
Also remember I told you that viewmat always returns i.0 0 (in all non-exceptional cases in the standard J session manager).
Therefore, when you type
(viewmat ?~17) -: (viewmat #:i.63)
Here’s what -: sees:
(i.0 0) -: (i.0 0)
1
And that’s *all* that it sees. How can it answer anything but 1?
I suppose, to reconcile your intuition while not trying to come up with some whacky side-effect-checking version of -: (which is not possible in the general case), we could redefine viewmat to return the set of GL commands (or whatever) it ultimately issued to the operating system, or even just the interpolated numeric matrix it used to generate those commands, so long as that’s a functional mapping.
But if we did that, then you’d see something like
ifRGB__a=:0[a=.'' conew'jviewmat'[require'viewmat'
'' getvm__a #:i.63
+-----------+-----------------------------------------------------++-------+
|0 0 0 0 0 0| 0 0 0 0 0 0||viewmat|
|0 0 0 0 0 1| 0 0 0 0 0 16777215|| |
|0 0 0 0 1 0| 0 0 0 0 16777215 0|| |
|0 0 0 0 1 1| 0 0 0 0 16777215 16777215|| |
|0 0 0 1 0 0| 0 0 0 16777215 0 0|| |
|0 0 0 1 0 1| 0 0 0 16777215 0 16777215|| |
...
in your session, *every single time* you used viewmat. Wouldn’t you find that distracting?
Anyway, because -: is just comparing i.0 0 to i.0 0 and telling me they’re equal, no, I do not find the result 1 jarring.
-Dan
[1] J Vocabulary entry for ;: :
http://www.jsoftware.com/docs/help803/dictionary/d332.htm <http://www.jsoftware.com/docs/help803/dictionary/d332.htm>
More information about the Programming
mailing list