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