One-dimensional cellular automata: Difference between revisions
Content added Content deleted
(Added Scheme) |
(Added Scala) |
||
Line 1,100: | Line 1,100: | ||
..##.....#.......... |
..##.....#.......... |
||
..##................</pre> |
..##................</pre> |
||
=={{header|Scala}}== |
|||
{{works with|Scala|2.8}} |
|||
def cellularAutomata(s: String) = { |
|||
def it = Iterator.iterate(s) ( generation => |
|||
("_%s_" format generation).iterator |
|||
sliding 3 |
|||
map (_ count (_ == '#')) |
|||
map Map(2 -> "#").withDefaultValue("_") |
|||
mkString |
|||
) |
|||
(it drop 1) zip it takeWhile Function.tupled(_ != _) map (_._2) foreach println |
|||
} |
|||
Sample: |
|||
<pre> |
|||
scala> cellularAutomata("_###_##_#_#_#_#__#__") |
|||
_###_##_#_#_#_#__#__ |
|||
_#_#####_#_#_#______ |
|||
__##___##_#_#_______ |
|||
__##___###_#________ |
|||
__##___#_##_________ |
|||
__##____###_________ |
|||
__##____#_#_________ |
|||
__##_____#__________ |
|||
</pre> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{Works with|Scheme|R<math>^5</math>RS}} |
{{Works with|Scheme|R<math>^5</math>RS}} |