On Wed, Mar 23, 2011 at 10:11 AM, Adrian May
<[email protected]> wrote:
> Yippee! I got my first J program working. Here it is:
>
> ((];(0.5*((<1)&{::*/(<0)&{::)@(((%&2@:+&.>)(<0)&{::)~)(+)((+
> 1-~2*?@:(2"0))@(*&0.5)@((<1)&{::)@[)))((><1)&{::(];((_1&[)`(1&[)@.(>:&0))"0@(+/
> .*))((1-~2*?@:(2"0))&[)`((_1 _1)&[)@.((*/ .=)&(_1
> 1))@((0;1)&{::)))@([(1!:2&2)@(8!:0)@((0;0)&{::,(0;1)&{::))^:40((0 0;0 0);2
> 2$0)
That's a bit hard to read, for me.
You can assign names to the various components you are using. For example:
seed=: (; ; ,:)~ 0 0
io=: (<0)&{::
weights=: (<1)&{::
outputs=: (0;1)&{::
insNouts=: (0;0)&{:: , (0;1)&{::
Note, however that insNouts could have been defined as ;@io so perhaps
it does not deserve its own name.
show=: 1!:2&2@(8!:0)
trace =: [ show@;@io
...
You can usually have the interpreter discard the names you do not need
by using f. on your final verb. For example consider:
trace f.
[ 1!:2&2@(8!:0)@;@(0&({::))
I am a bit surprised that we get ({::) instead of just plain ({::)
here, but I imagine that is also something that can be fixed later.
(This is with my display configured for linear form, which basically
means serialized without redundant parenthesis -- though the verbs of
gerunds are not displayed this way, even when they are the argument of
an operation which consumes gerunds. But I suppose that's something
else to fix, when one of us gets time.)
Also, instead of using
(*/ .=)&(_1 1)
I think I would instead use -:&_1 1 (though this also depends on what
your later plans are, but this kind of change is easy enough to undo).
And, in case you had not noticed: a list of numbers (which contain
spaces) is considered to be a single word by the parser (just as a
quoted character list which contains spaces is considered to be a
single word by the parser).
Anyways, here's a few more example definitions:
random=: _1 1 {~ ?@2:"0
constant=: _1 _1&[
nextPair=: random`constant@.(-:&_1 1)
combiningFn=: ] ; _1 1 {~ >:&0@(+/ .*)
But I am displaying my ignorance here, with useless names like
nextPair and combiningFn. Still, with these words your original
sentence could be rephrased as:
((] ; 0.5 * */&>/@(-:@+&.> io)~ + (+ random)@-:@weights@[) (weights
combiningFn nextPair@outputs))@trace^:40 seed
> I'm a bit confused about this bit:
>
> ((1-~2*?@:(2"0))&[)`((_1 _1)&[)@.((*/ .=)&(_1 1))
>
> because
>
> 1-~2*?@:(2"0) is magically producing a length 2 list like I want it to. How?
> Is it getting a list fed to it as a parameter? I know that would do the
> trick, but how's it getting in? I know the parameter gets fed to (rand&[),
> but why would it go deeper?
Yes -- You are using 2"0 here, and when given a two element list for
an argument it will produce a two element list for its result:
2"0 'ab'
2 2
An alternate way of expressing what you want for that case might be 2
2"_ which will always produce a two element list regardless of the
length of its argument:
2 2"_ 'abc'
2 2
That said, you have that phrase (which corresponds to my word
'random') in two places in your code.
In one place it is getting a single element argument, as you can see
by executing this code:
((] ; 0.5 * */&>/@(-:@+&.> io)~ + (+
random&([smoutput&<))@-:@weights@[) (weights combiningFn
nextPair@outputs)) seed
In the other place it is getting a two element argument, as you can
see by executing this code:
nextPair=: random&([smoutput)`constant@.(-:&_1 1)
((] ; 0.5 * */&>/@(-:@+&.> io)~ + (+ random)@-:@weights@[) (weights
combiningFn nextPair@outputs)) seed
As for what that two element list is -- I hope you can see, in this
rephrased code, that it's the result of the function I have named
'outputs'. In other words it's (0;1){:: seed (or some function of
seed which has the same structure of seed).
That said, I should also note that there is another way of
representing very long expressions in J, breaking them out across
multiple lines:
program=: ''1 :(0 :0-.LF) f.
(
] ; 0.5 * */&>/@(-:@+&.> io)~ + (+ random)@-:@weights@[
) (
weights combiningFn nextPair@outputs
) NB.
)
program@trace^:40 seed
...
I have to be careful because an isolated right parenthesis terminates
the definition (and is not a part of the definition). And I am really
just building a very long line (so I can only have NB. at the very end
of the definition). So puts some constraints on how you can style
your code. But if you feel compelled to define very long sentences,
perhaps this approach (where I am explicitly eliminating what would
normally be ends of sentences) could be useful for you.
I hope some of this helps.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm