문제

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?

도움이 되었습니까?

해결책

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top