Domanda

I am wondering why the following failed to work.

   (1=$:@([:+/[:*:@"."0":)`]@.(e.&1 4))7
1

and

   (({&('no';'yes'))@(1=$:@([:+/[:*:@"."0":)`]@.(e.&1 4)))7
┌──┐
│no│
└──┘

I would expect it to return yes since it is taking the second argument (because the first expression evaluates to 1). Could anybody please explain?

Thanks.

È stato utile?

Soluzione

I must admit that it is difficult to figure out what you are trying to do, but the issue i think is your use of Self-Reference ($:).

The description of Self-Reference in the J dictionary states '$: denotes the longest verb that contains it.' http://www.jsoftware.com/help/dictionary/d212.htm

By combining ({&('no';'yes')) with your original verb

(1=$:@([:+/[:*:@"."0":)`]@.(e.&1 4))

You have actually changed the verb that $: self references and I believe this results in the (unexpected) behaviour. I hope that this helps. If you can give a reason for using $: I might be able to give a better answer as to why your verb works this way. I tend to use it very little, as there are usually better solutions in J.

Just another thought. You could define the parts of the verb you are looking to create separately and this would limit the scope of the $: and give you the behaviour that you would like.

   choose=: (1=$:@([:+/[:*:@"."0":)`]@.(e.&1 4))
   display=:({&('no';'yes'))
   display @ choose 7
┌───┐
│yes│
└───┘
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top