I think Henry's JfC book is a great and have leanrt much from it
however I am puzzled by his chapter on Loopless Code VI: Temporay
Variables where he says that
"The algorithms that are hardest to put into loopless form are the
ones that chug through the items of an array, modifying a set of
temporary variables that control the operation at each step" and he
then presents his knave example and uses his special LoopWithInitial
conjunction and so on. (PS his example does not seem different from
fibonacci calculations either)
Well to me this does not look like a difficult one to put in j terms
and the obvious approach is to use iteration (in the FP sense) using
the built-in Power conjunction (rather than create a new conjunction)
I will simply present the code which anyway seems easier to explain
than this chapter in general
The data
p =. 100 25 100 50 5 10
k =. _2 ]\ 50 50 100 25 5 10 25 50 25 10
To do one step
2}.\:~ p,{.k
50 50 50 25 10 5
To do one step and carry over the temporary data as well
(2}.\:~ p,{.k);}.k
┌────────────────┬──────┐
│50 50 50 25 10 5│100 25│
│ │ 5 10│
│ │ 25 50│
│ │ 25 10│
└────────────────┴──────┘
Therefore we need to box the intial data and generate a new 2 item box
on each step and can make an explicit verb (which can be made tacit)
that expects p;k as y
knave1=: 3 : '(2}.\:~ (>{.y),{.>{:y);}.>{:y'
knave1 data
┌────────────────┬──────┐
│50 50 50 25 10 5│100 25│
│ │ 5 10│
│ │ 25 50│
│ │ 25 10│
└────────────────┴──────┘
We now use the power conjunction to perform the iteration (and I have
generalised to handle different k items)
>{. knave1^:(#k) p;k
25 10 10 10 5 5
This does not store data on what the knaves gained in the process but
that was not the task at hand, the above could be modified to suit.
Henry at the end of this chapter says 2 things
a) "You may object that since the left operand of LoopWithInitial is
applied to each item, it still has to be interpreted for each item, so
nothing is gained by avoiding the loop. An astute observation, but in
the second part of this book we will learn how to write verbs that are
not reinterpreted for each item."
Does this apply here? Does knave1 have to be tacit to avoid this? It
can be made tacit
knave1t=: 13 : '(2}.\:~ (>{.y),{.>{:y);}.>{:y'
knave1t
(2 }. [: \:~ ([: > {.) , [: {. [: > {:) ; [: }. [: > {:
b) "Finally, you may observe that the temporary vector, which only
needs to be a list, turns into a rank-2 array by the time we have
passed it through each item. Doesn't that waste a lot of space? Yes,
it does, and if your problem is big enough that the space matters, you
may have a valid application for a loop."
The above solution which IMHO seems more straighforward than his does
not have this problem. So not sure if his implication still follows.
Using Time and Space calculations shows more (no I did not create
repeat arrays for the calculations but the resutls below seem
consistently relatively stable)
Henry's explicit version
ts=: 6!:2 , 7!:[EMAIL PROTECTED]
ts '>{. knave1^:(#k) p;k'
6.76064e_5 4736
ts '>{. knave1t^:(#k) p;k'
4.44191e_5 2752
--
martin
http://impartialism.blogspot.com
"Reality is that which, when you stop believing in it, doesn't go away" PK Dick
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm