This is not a good candidate for the power conjunction.

You could do it with the power conjunction but it's not a good fit.

The reasons for this are:

(1) You have two independent values you are working with - the value
you use for flow control is independent of the value you are passing
to your functions e and o

(2) The expression itself produces no useful result.

So this means that at every step of the way you are working against
the design of the power conjunction. There is no elegance to be had,
and probably a better approach would be to indent your explicit
definition.

t =. monad define
  n =. 20
  while. n =. n - 1 do.
    if. 2|n do. smoutput y =. e y
    else. smoutput y =. o y
    end.
  end.
)

That said, a if you wanted to rephrase this tacitly, here are the
steps I would take:

First, since you really want the side effects, define variations on e
and o which display their result:

   E=:1!:2&2@e
   O=:1!:2&2@o

Second, looking at your implementation of t, you have a total of 19
times through the loop. You have 10 'e' invocations and 9 'o'
invocations. So I'd set this up as a train:

   (36$[:`E`[:`O)`E`:6

But, also, your result is the result that smoutput would produce, so:

   T=: 0 0 $ (36$[:`E`[:`O)`E`:6
   T 100
98
97
95
94
92
91
89
88
86
85
83
82
80
79
77
76
74
73
71

Good enough?

Well, maybe not... since your 'e' happens for the odd values of N, and
your 'o' happens for the even instances, I'd be tempted to rename
them.

Thanks,

-- 
Raul


On Mon, Jul 13, 2015 at 12:39 PM, Brian Schott <[email protected]> wrote:
> I have the following verb t and wonder if anyone would like to show how it
> could be more elegantly defined (using the Power conjunction?) I would
> enjoy learning your result.
>
> The intent of t is to alternatively subtract 1 or 2 from a positive number
> showing the intermediate results of say n=20 subtractions.
>
>
>    e =. -&2
>    o =. -&1
>
>    t =. monad define
> n =. 20
> while. n =. n - 1 do.
> if. 2|n do. smoutput y =. e y
> else. smoutput y =. o y
> end.
> end.
> )
>    t 100
> 98
> 97
> 95
> 94
> 92
> 91
> 89
> 88
> 86
> 85
> 83
> 82
> 80
> 79
> 77
> 76
> 74
> 73
> 71
>
>
> --
> (B=)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to