[Jprogramming] Learning Recursion
Louis de Forcrand
olius at bluewin.ch
Thu Sep 29 20:02:32 UTC 2016
Try "tacitifying" rm. If you get stuck, you can use (13 :) , but a usual tacit definition of rm would be slightly different.
I suggest you take a good look at the dictionary pages on trains and tacit verbs. Here's the way I would write rm as a tacit verb:
rmt=: ] #~ (| i.@#) ~: <:@[
x rmt y
x (] #~ (| i.@#) ~: <:@[) y
(x ] y) #~ (x (| i.@#) y) ~: x <:@[ y
y #~ (x | i.@# y) ~: <: x [ y
y #~ (x | i. # y) ~: <: x
Be careful with @ and & vs @: and &: . Read the dictionary carefully.
About for_xyz. : you could simply use
for_e. x do. s=. e rm y end.
This would be read as
"for each item e of x (in order of occurence) do s=. e rm y".
Tacitly, I would use a fold:
mlmt=: [: > <"0@|.@[ rmt&.>/@, <@]
This works in a similar way to that described by Justin. mlmt boxes x's scalars, and appends boxed y as the accumulator. x is reversed, since J's / evaluates from right to left, with the accumulator as right arg.
In fact the adverb
foreachT=: 1 : '[: > <"_1@|.@[ u&.>/@, <@]'
is equivalent to
foreachE=: 1 : 0
for_e. x do. y=. e u y end.
)
I hope I didn't make any mistakes.
Happy tacitification,
Louis
> On 29 Sep 2016, at 04:52, Skip Cave <skip at caveconsulting.com> 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" <jrslepak at ccs.neu.edu> 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 <skip at caveconsulting.com>
>> To: Programming at jsoftware.com
>> 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
More information about the Programming
mailing list