Unpredictable behavior? The second version of mergerow seems like it should give exactly the same results as the first. Any problems it causes seem like cases for further debugging.
Here's a version that doesn't rely on rplc, if it helps. mergerow =: ((*>:) #~ _1|.-.@]) [:>/\.&.|. 0,~ 2 =/\ ] mergerow 3 4 4 4 4 4 5 8 8 7 3 8 8 4 5 16 7 The tricky part is suppressing extra matches. My initial solution used a nice trick for counting within groups of ones: ]m =. 0,~ 2 =~/\ 3 4 4 4 4 4 5 8 8 7 0 1 1 1 1 0 0 1 0 0 ([*+)/\.&.|. m 0 1 2 3 4 0 0 1 0 0 2|([*+)/\.&.|. m 0 1 0 1 0 0 0 1 0 0 Since we only need the answer mod 2, we can shift the 2&| inside, and note that (2|+) is the same as (~:) on binary inputs. Then ([*~:) turns out to be equal to (>) on binary inputs, a fact I found just by computing the truth table. ([*~:)/\.&.|. m 0 1 0 1 0 0 0 1 0 0 ([*~:)"0/~ 0 1 0 0 1 0 >/\.&.|. m 0 1 0 1 0 0 0 1 0 0 Marshall On Wed, Jun 08, 2016 at 09:15:14AM +1200, Ric Sherlock wrote: > I'm looking for a J-like approach to a verb for merging adjacent numbers in > a row where the numbers are the same. For example > > mergerow 4 4 4 4 > > 8 8 > > mergerow 8 8 > > 16 > > mergerow 16 > > 16 > > mergerow 4 4 4 > > 8 4 > > mergerow 8 4 > > 8 4 > > mergerow 4 8 8 4 > > 4 16 4 > > > My version below which I've used as part of the answer to a RosettaCode task > <http://rosettacode.org/wiki/2048#J> is very non J-like: > > mergerow=: verb define > len=. <:@# y > row=. i. i=. 0 > while. (i <: len) *. len >: 0 do. > if. =/ (i , i+1) { y,_9 do. > row=. row, +: i { y > i=. i + 2 > else. > row=. row, i { y > i=. i + 1 > end. > end. > row > ) > > I thought I had a good solution with: > > mergerow=: rplc (2&# ; +:)&>@~. > > But this seemed to cause unpredictable behaviour when used as part of the > whole solution. > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
