This is just something quick I wanted to for about a week now: an interpreter for Markov algorithms. So I wrote two of them, one in Ruby to get the idea how to do this and one in Javascript to have it on the web. Both are not particularly beautiful written but get the job done. Click this neat little link to get to the online version or have a look at the Ruby code (as you can see the production system and input word are “hard coded”). In both versions the production system removes the trailing zeros from the input word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | E = 0x00 P = [ { "a1" => "1a" }, { "a0" => "0a" }, { "0a" => "a" }, { "a" => [E] }, { E => "a" } ] I = "0101101001110110000" word = I while true do ruleToRun = nil P.each {|rule| rule = rule.to_a[0] score = word.index(rule[0]) if (!score.nil? || rule[0] == E) && ruleToRun.nil? ruleToRun = rule end } break if ruleToRun.nil? shouldStopAfterThisRule = ruleToRun[1].is_a? Array ruleToRun[1] = ruleToRun[1][0] if shouldStopAfterThisRule oldword = word if ruleToRun[0] == E word = ruleToRun[1] + word; elsif ruleToRun[1] == E word = word.sub(ruleToRun[0], "") else word = word.sub(ruleToRun[0], ruleToRun[1]) end puts "#{ruleToRun[0]} -> #{(shouldStopAfterThisRule ? "." : "")}#{ruleToRun[1].to_s}: #{oldword} => #{word}" break if shouldStopAfterThisRule end puts word |

where
is the radius of this chain ball,
is the set of received particles for this chain ball and
is the particle radius