Hello,

   I use state machines a lot in my programs (in other
   languages). I am trying to understand how I can use J for
   those purposes. I have read the Sequential Machines and
   Huffman Coding labs. But I am unable to see how to solve this
   toy problem (without using regexp):

   Input alphabet {a,b,c}
   I want to replace runs of 'a' with the word 'alpha', runs of 'b'
   with the word 'beta', and leave the 'c's unchanged.

   For example, abbbbaaacccaaaaabbbb becomes alphabetaalphacccalphabeta.

   The state diagram is like this, where the arcs are labelled as
   inp/out.


[Cannot inline image]

   I have created the input map successfully (not sure if it is a
   good way, though):

   makemap =. 3 : '+/ (>:i.#y) *"0 1 a.="1 0 y'
   m=.makemap 'abc'

   I guess that this can be achieved using only outputs 0 and
   2, since I am just interested in the runs.


   Also, since the output is not just a part of the input, f
   must be 2 or 3 or 4.

   But I am stuck at this point. If I use f=.2, then I can get
   a list of boundaries of all the runs. If I have to write another
   verb that will convert this list to the desired output, then
   basically I have to implement a simplified version of the same
   state machine inside that verb, which does not look good.

   f=.3 does not look promising either, since for 'c' I need to
   know the length of the run.

   How should I proceed?


Thanks and regards,

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

Reply via email to