the problem
This is a recurring difficulty I encounter when I reach for a (u^:v^:_ y) (Do-While) construct.
As context, it was inspired by this code golf question, which asks you to build up a string from integers, starting with 0, appending the stringified form of the next integer, until the length reaches the number given.
For example, if we stop at length 5, we build up the string 01234 (which has length 5). The last number appended was 4, hence we return 4 as our answer. If we were to stop at length 12, we would produce the 12-character string 012345678910, and hence return 10.
ugly solution
Here is a helper verb len which, given a list of integers, will return the total length of their representation as a ''-joined string:
len =. +/ @: (#@":"0)
The following expression will then return the correct puzzle answer for input 12:
(>: ^: (<&12@len@:i.@>:) ^:_) 0 NB. returns 10
question
The problem is that I have hard-coded my input 12 into the expression, rather than accepting it as an argument to the tacit verb.
Yet I don't see how to correct this. The "Do" verb -- the incrementing counter -- is the one that receives the argument. Since it always starts at 0, it should be hardcoded, rather than hardcoding the stopping length. But because of the semantics of "Do-While", I don't see how I'd do that, because it accepts an argument to the main verb u, not the condition verb v.
Is there an elegant way to solve this?
Would standard recursion with $: and @. be a good strategy?
I know I could make the argument a list containing both the "stopping length" and the counter, but that will just clutter the code with bookkeeping verbs like {. and {: as I tease apart and piece together the list on each iteration.
bonus question
In the notes for "Do-While", NuVoc warns, "Despite its name, Do-While is very rarely used in J. Almost all looping is done using the other modifiers", suggesting there might be another approach altogether.
I don't see how that's possible in this case without the unacceptable method of first guessing at an upper-bound for the answer, but if there is, I'd like to know.