Yes, it's much simpler this way if you have a scalar accumulator and scalar arguments, but I wanted to demonstrate the boxed version to avoid depending on the accumulator having any particular shape.
--- Justin Slepak PhD student, Computer Science dept. ----- Original Message ----- From: 'Pascal Jasmin' via Programming <[email protected]> To: [email protected] Sent: Wed, 28 Sep 2016 20:34:43 -0400 (EDT) Subject: Re: [Jprogramming] Learning Recursion lispy code, as I call it, (head/rest patterns) has a bit simpler implementation than you gave (2&}. ,~ {. + {.@}.)(^:3) 20 2 3 4 29 or (2&}. ,~ +/@:(2&{.) )(^:3) 20 2 3 4 29 and of course ignoring, +/ 20 2 3 4 ----- Original Message ----- From: Justin R. Slepak <[email protected]> To: [email protected] Sent: Wednesday, September 28, 2016 7:08 PM Subject: Re: [Jprogramming] Learning Recursion 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
