I want to write prime function for purposes of learning J. So far I've come up with this:
=&0+/(=&0)(2+i.(-&2)y)|y
It's working great except that I should store number in y
variable.
y=.5
=&0+/(=&0)(2+i.(-&2)y)|y NB. prime cheker
1
y=.13
=&0+/(=&0)(2+i.(-&2)y)|y NB. prime cheker
1
y=.14
=&0+/(=&0)(2+i.(-&2)y)|y NB. prime cheker
0
How do I write a function that works what takes argument? i.e. f 13
-> 1
You can just define a verb using
: 3
.When using
: 3
,y
always refers to the right hand argument of the verb.If you want to define a dyadic verb, use
: 4
andx
for the left argument.Btw, you can set the value of a variable anywhere: