Here's a generalized approach (not necessarily better, but convenient
if you need a variety of these kinds of sequences):

Alternating=: (1 :0)("0)
  (+#) {. (, (altSeq/m)`:6)@]^:((#m) >.@%~ [)
)

altSeq=:4 :0
  (, x`:6@{:)@(y`:6@{:)`''
)

Here's a couple examples:

   9 +:`>: Alternating 1
1 2 4 5 10 11 22 23 46 47
   9 ]`+:`>: Alternating 1
1 2 4 4 5 10 10 11 22 22

Note that (Alternating) is a parse time word.  The phrase (+:`>:
Alternating) builds something that can be thought of as code, much
like a compiler builds code.  But if you inspect this result you'll
see some differences from my original post.

   +:`>: Alternating
((+ #) {. (, (, +:@{:)@(>:@{:))@]^:(2 >.@%~ [))"0

Can you think of why I might have introduced these complications? I
think that one of my changes is questionable and should perhaps be
removed or changed - do you have an idea of which change that would be
(and can you talk about why it should be changed)?

Thanks,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to