I was messing around with closures, looking for something interesting to do (other than the obvious JAPH or quine) and came up with this...

```#!/usr/bin/perl -- -*- cperl -*-
my%t=('H'=>[99,6,0,3,4,0,1],'*'=>[5,3,1,2,1,2,11],'O'=>[7,9,0,4,8,3,4]
,'x'=>[12,3,0,1,9,2,4],'+'=>[17,3,0,1,9,2,4],);sub _{my(\$x,\$y,\$t)=@_;\$
g[\$x][\$y]=l(\$t,\$x,\$y)}sub l{my(\$t,\$x,\$y,\$a)=@_;+{v=>\$t,i=>sub{++\$a;my(
\$n,\$s,\$o);my@t=@{\$t{\$t}};for\$u(-1..1){for\$z(-1..1){\$n=\$g[\$x+\$u][\$y+\$z]
;if(\$n->{v}eq\$t){++\$s}elsif(\$n->{v}){++\$o}}}if(\$s<\$t[2]or\$s>\$t[1]or\$o>
\$t[4]or\$o+\$s<\$t[5]or\$a>\$t[0]){undef\$g[\$x][\$y]}if(rand(10)<\$t[6]or\$s==\$
t[3]){my(\$v,\$w)=map{rand(42)%3-1}0..1;\$v+=\$x;\$w+=\$y;\$g[\$v][\$w]=l(\$t,\$v
,\$w)if not\$g[\$v][\$w];}}}}map{_(\$\$_[0],\$\$_[1],"H")}([20,10],[20,9],[21,
10],[21,9]);_(40,10,'*');_(39,10,'*');map{my\$q=\$_;map{_(\$_,\$q,'O')}(29
,30)}(18,19);_(30,3,"+")^_(29,3,"x");while(\$:){&c;for\$y(1..22){for\$x(1
..60){if(\$m=\$g[\$x][\$y]){print\$\$m{v};\$\$m{i}->()}else{print" "}}print\$/}
print\$/;sleep 1}sub c{`clear`}#To support Win32 make that sub c{`cls`}

If you let it run, it becomes obvious relatively quickly that the fast-growth type won't last, but it takes a bit longer to see what happens between the others. See if you can predict it before it becomes blindingly obvious. (Hint: it's not just how any two types interact with eachother, but how they interact with the others, too. For example, if you remove the symbiotic pair, the interaction between the close-packing and slow-growth types changes.)

Okay, so it's not the most original concept ever... but hey, when I did it in QBasic in 1994, it didn't use a functional paradigm, so that's something.

```\$;=sub{\$/};@;=map{my(\$a,\$b)=(\$_,\$;);\$;=sub{\$a.\$b->()}}
split//,".rekcah lreP rehtona tsuJ";\$\=\$ ;->();print\$/

Replies are listed 'Best First'.
Re: functional cellular automata
by simonm (Vicar) on Sep 19, 2003 at 17:41 UTC
Nice! I haven't seen many multi-type CAs in the past, and they clearly have a lot of potential.
I haven't seen many multi-type CAs in the past

Those are the only ones that really interest me; the single-type ones usually don't have the same sorts of complex interactions.

If you want to see the interactions change, play with the numbers in the arrays in the values of %t (where it is defined at the beginning of the script). These numbers effect how each type grows or reproduces, how long-lived each cell can be, and how easily they die from isolation or crowding. Not necessarily in that order, of course. The numbers in the version I posted give a fairly interesting scenerio, but I'm sure there are other possible sets of interaction to uncover. For example, maybe the symbiotic + and x types are too robust, and perhaps the fast-growth type is too wimpy. Perhaps if their numbers were tweaked a bit, you could get a more stable environment where they can all co-exist for a longer period of time...

```\$;=sub{\$/};@;=map{my(\$a,\$b)=(\$_,\$;);\$;=sub{\$a.\$b->()}}
split//,".rekcah lreP rehtona tsuJ";\$\=\$ ;->();print\$/