cobbal
cobbal

Reputation: 70805

J @ not working as expected

I'm just starting to try to pick up the J language, and am confused by the following:

   1 2 +/@{ i.4
1 2
   +/ 1 2 { i.4
3

when in the documentation for @ it says: "x u@v y ↔ u x v y"

I assume I'm just mistaking one part of speech for another, but can't figure it out

also, how do I tell what type of speech a name is?

Upvotes: 1

Views: 278

Answers (3)

rdm
rdm

Reputation: 688

Wikipedia has, in my biased opinion, a decent writeup on rank, and what it means in the context of the different parts of "speech" in J.

But to answer the original question, J's trace facility can be useful for understanding how its grammar works:

   require'trace'
   trace '1 2 +/@{ i.4'

This will take you step by step through the parsing process, showing the words being consumed by each production rule and the result each generates.

Upvotes: 2

ephemient
ephemient

Reputation: 205034

   NB. u b. 0 returns the rank of u
   NB. the rank of a verb determines the arguments it applies to at a time
   NB. monadic + y applies to atoms; dyadic x + y applies to pairs of atoms
   + b. 0
0 0 0
   NB. monadic +/ y and dyadic x +/ y apply to anything (unbounded rank)
   +/ b. 0
_ _ _
   NB. monadic { y applies to arrays of atoms;
   NB. dyadic x { y applies to pairs of atoms and anything
   { b. 0
1 0 _
   NB. u @ v has the rank of v
   +/@{ b. 0
1 0 _
   NB. since 1 2 { i.4 returns atoms at a time, +/ works on atoms
   +/"0 [ 1 2 { i.4
1 2
   NB. u @: v has unbounded rank
   +/@:{ b. 0
_ _ _
   NB. +/ applies to all of 1 2 { i.4 at once
   +/"_ [ 1 2 { i.4
3

   NB. mechanical translation to tacit form
   13 : '+/ x { y'
[: +/ {

Upvotes: 4

cobbal
cobbal

Reputation: 70805

Ah, I think I may have figured it out, I need to use @: instead of @

   1 2 +/@:{ i.4
3

which is what I wanted. Guess I'm going to have to read up some more on rank, which is the only difference between @ and @:

Upvotes: 1

Related Questions