To follow up on the message I sent off-list, here's some perspective
on how to construct an inductive implementation of remove-every-nth.
First, let's get a non-numeric list to remove every nth, from:
'abcdefghij'
abcdefghij
Second, if we are going to remove every third element, inductively,
we'd probably want to start like this:
(<<<8){'abcdefghij'
abcdefghj
And, when we are done, we would have achieved this:
(<<<2){(<<<5){(<<<8){'abcdefghij'
abdeghj
Double checking:
rm =:4 :'(-.(<:x)=x|i.$y)#y'
3 rm 'abcdefghij'
abdeghj
So our inductive verb would probably be working with a pair of boxes,
the first being a triple boxed integer and the second being the string
we are working on. The result would have to be a similar pair of
boxes.
So our verb would do two things. It would subtract its left argument
from the integer nested deep in the first box of the right argument
for the first box of the result. It would also use { inside the boxes
to modify the list that will be in the second box. And we can glue
those two verbs together using a comma.
We can use L:0 to deal with the boxing for the left part of the verb,
and &.> to deal with the boxing for the right part of the verb. This
means our integer should be nested four levels deep (because { is
going to want it nested three levels deep and &.> is going to strip
away one of those levels).
Also, the left part of the inductive verb is going to be dyadic, but
we want to it to ignore the string. We can do that using u&{. (which
will pass through the left argument unmodified, and get only the first
box of the right argument). Meanwhile, the right part of the inductive
verb needs to ignore the inductive left argument and needs to apply
{&.> between the pair of boxes of the right argument. We can use a
v/@] sort of thing to achieve that.
Putting that all together, we get something like this, maybe:
3 (-~L:0&{., {&.>/@]) (<<<<8),<'abcdefghij'
Or, stripping out the example data, our I think we want our inductive
verb (the left argument to ^:) to look like this:
(-~L:0&{., {&.>/@])
Hopefully my thought process in putting this together makes sense.
And, hopefully you can kind of see the sort of pieces you would need
for the rest of the sentence to make this work?
Thanks,
--
Raul
On Wed, Sep 28, 2016 at 10:52 PM, Skip Cave <[email protected]> wrote:
> Justin,
> Very nice explanation. I learned quite a lot from your clear directions.
> However, I for now I think I will try an explicit solution using the
> auto-indexing for_. do. to step through a right argument vector.
>
> Here's a nth place removal verb (dyadic)
> rm =:4 :'(-.(<:x)=x|i.$y)#y'
>
> NB. Test removal verb
>
> il =: >:i.10 NB. list
>
> 2 rm il NB. remove every second in list
>
> 1 3 5 7 9
>
> 3 rm il NB. remove every third in list
>
> 1 2 4 5 7 8 10
>
> 3 rm 2 rm il NB. remove every 2nd then every 3rd in list
>
> 1 3 7 9
>
> NB. Multi-pass Removal Verb (Dyadic)
>
> NB. Left arg is removal counts (vector) The for_n. Loop is very handy here
> (using n_index)
>
> NB. Right arg is the list (vector)
>
> mlm =: 4 :0
>
> s =:y
>
> for_n.x do.s=:(n_index{x)rm s end.
>
> )
> NB. Test multi-pass removal verb
>
> 2 3 mlm il
>
> 1 3 7 9
>
> 3 4 mlm il
>
> 1 2 4 7 8 10
>
> 2 3 4 mlm il
>
> 1 3 7
>
> 3 1 mlm il NB. Any '1' in the removal count removes the whole list.
>
> On Sep 28, 2016 6:08 PM, "Justin R. Slepak" <[email protected]> wrote:
>
>> The tricky bit is that ^: isn't a very flexible recursion scheme. The verb
>> it repeatedly applies effectively has to be monadic. A function on two
>> arguments can be turned into a function on one argument if we make that
>> argument an ordered pair. So to make addition monadic, we'll start by
>> splitting a vector into two pieces:
>> ({. + {:) 1 2
>> 3
>>
>> We'll designate the head of our two-element vector as the "accumulator"
>> and use the tail as the TODO list -- the arguments for later. Our monadic
>> adder must extract the head of the tail and add it to the accumulator (the
>> head).
>> ({. + ({.@{:))
>>
>> Unfortunately, our accumulator and argument list have different shapes.
>> We'll have to box them in order to put them into the same vector.
>> (<20),(<2 3 4)
>> ┌──┬─────┐
>> │20│2 3 4│
>> └──┴─────┘
>>
>> We also have to extract the TODO list, unbox it, then take its head. Then
>> we can add it to the accumulator.
>> ((>@{.) + ({.@>@{:)) (<20),(<2 3 4)
>> 22
>>
>> Yay, we have an updated accumulator! We also need to update our TODO list.
>> That's just beheading the current TODO list.
>> (}.@>@{:) (<20),(<2 3 4)
>> 3 4
>>
>> Both the accumulator and the new TODO list will have to get boxed up again
>> and recombined.
>> ((<@((>@{.) + ({.@>@{:))) , (<@(}.@>@{:))) (<20),(<2 3 4)
>> ┌──┬───┐
>> │22│3 4│
>> └──┴───┘
>>
>> So let's apply *that* big verb three times:
>> (((<@((>@{.) + ({.@>@{:))) , (<@(}.@>@{:))) ^:3) (<20),(<2 3 4)
>> ┌──┬┐
>> │29││
>> └──┴┘
>>
>> From here, extracting the final value of the accumulator is easy.
>>
>> None of this relies on anything special about the + verb, so this
>> transformation should be packageable as an adverb if you're really intent
>> on using ^: everywhere.
>>
>> ---
>> Justin Slepak
>> PhD student, Computer Science dept.
>>
>> ----- Original Message -----
>> From: Skip Cave <[email protected]>
>> To: [email protected]
>> Sent: Tue, 27 Sep 2016 03:35:19 -0400 (EDT)
>> Subject: [Jprogramming] Learning Recursion
>>
>> NB. I am attempting to learn recursion using ^:
>>
>>
>> NB. A simple recursion:
>>
>>
>> 2+^:(3) 2
>>
>> 8
>>
>>
>> NB. This is equivalent to
>>
>>
>> 2+2+2+2
>>
>> 8
>>
>>
>> NB. What if I want to change the left argument on each recursion?
>>
>> NB. Instead of 2 each time, I want to add 2, then 3, then 4.
>>
>> NB. I know there are much easier ways to do this, but I want to see
>>
>> NB. if I can avoid do. while. loops if the left argument changes on each
>> iteration
>>
>>
>> NB. I tried
>>
>>
>> (2 3 4)+^:(3) 2
>>
>> 8 11 14
>>
>>
>> NB. Nope. That didn't work.
>>
>> NB. What I want to achieve is:
>>
>>
>> 2+3+4+2
>>
>> 11
>>
>> NB. but do it recursively using ^:
>>
>> Skip
>>
>> Skip Cave
>> Cave Consulting LLC
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm