David Wallin wrote:
> I'm pretty new to J and I'm still struggling a bit with the syntax.
> I was trying to write a small 'mod' function that left negative
> numbers untouched.
> The result I expected is that of 'mod3', 'mod6' and 'mod7' below.
> If someone could give me some insights as to why the results differ
> I'd appreciate it.
>
> mod2=: ((|`] @. (<&0)))"0
> mod3=: ((]`| @. (>&0)))"0
> mod4=: ((|`] @. (<&1)))"0
> mod5=: ((]`| @. (>&1)))"0
> mod6=: ((|`] @. (]<0:)))"0
> mod7=: ((|`] @. (]<1:)))"0
Note that mod6 and mod7 include ] in their predicate, while
none of the others do. Rather than work through all of your
examples, let's just focus on mod2:
> 4 mod2 1-~i.6
> 3 0 1 2 3 0
This is equivalent to:
4 mod2 _1 0 1 2 3 4
or
4 (((|`] @. (<&0)))"0) _1 0 1 2 3 4
Focussing on the predicate:
4 (<&0) _1 0 1 2 3 4
0 0 0 0 0 0
Why is that?
Looking at the definition of &
http://www.jsoftware.com/books/help/dictionary/d630n.htm
the above line is equivalent to:
(<&0^:4) _1 0 1 2 3 4
which is equivalent to any of the following:
<&0 <&0 <&0 (<&0) _1 0 1 2 3 4
<&0 <&0 (<&0) 1 0 0 0 0 0
<&0 (<&0) 0 0 0 0 0 0
(<&0) 0 0 0 0 0 0
In other words, mod3 worked because >&0 on a boolean
vector was an identity operation. But note that you
would have gotten a domain error if you had a number
on the left which was not a non-negative integer.
> BTW, is there an IRC channel for J ?
I don't know. Personally, I gave up on IRC a long time ago.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm