How do I define a monadic function to work on a list in J?
Posted
by Gregory Higley
on Stack Overflow
See other posts from Stack Overflow
or by Gregory Higley
Published on 2010-05-19T21:15:01Z
Indexed on
2010/05/19
21:20 UTC
Read the original article
Hit count: 197
Let's say I have the following J expression:
# 3 ((|=0:)#]) 1+i.1000
This counts the number of numbers between 1 and 1000 that are evenly divisible by 3. (Now, before anyone points out that there's an easier way to do this, this question is about the syntax of J, and not mathematics.)
Let's say I define a monadic function for this, as follows:
f =: monad define # y ((|=0:)#]) 1+i.1000 )
This works great with a single argument, e.g.,
f 4 250
If I pass a list in, I get a length error:
f 1 2 3 |length error: f
Now, I completely understand why I get the length error. When you substitute the list 1 2 3
for the y
argument of the monad, you get:
# 1 2 3 ((|=0:)#]) 1+i.1000
If you know anything about J, it's pretty clear why the length error is occurring. So, I don't need an explanation of that.
I want to define the function such that when I pass a list, it returns a list, e.g.,
f 1 2 3 1000 500 333
How can I either (a) redefine this function to take a list and return a list or (b) get the function to work on a list as-is without being redefined, perhaps using some adverb or other technique?
© Stack Overflow or respective owner