Question

I don't understand well how J evaluate fork. In my case, I have this:

   v = ((*:+/)-(+/*:))

A simple code(but wrong), I tried to do the variance of a list:

   v i.11
|domain error
|((*:+/)-(+/*:))i.11

What I have done wrong?

Was it helpful?

Solution

The fork, per se, is correct. What's going wrong is the formulation of the tines (the outer verbs).

In J, +/ *: 10 11 12 is different from (+/ *:) 10 11 12 (try it). To make sum-of-squares a reusable function, you have to glue the two pieces together somehow.

There's several ways to do this in J. The most popular include using @: (compose function) and creating a pseudo-fork using [: (cap).

In your case, you could write either:

   v =: *:@:(+/)- +/@:*:  

Note the parens around (+/), which is required because of the /

or:

   v =: ([: *: +/) - ([: +/ *:) 

Note the parens around each tine: required to isolate them, so that they're treated as forks (technically the right tine does not need to be wrapped in parens, but doing so makes the whole structure more obvious and symmetric)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top