% $Id: Rules.oz,v 1.1 2011/04/21 12:31:11 leavens Exp leavens $ % Rules for working with one-dimensional cellular automata having 2 possible % values in each cell and a neighborhood of 3 cells (one on either side). % % A is represented by a function of type % % AUTHOR: Gary T. Leavens declare fun {ReversedBinaryDigitsOf N} fun {LeastBitOf N} if {IsOdd N} then 1 else 0 end end in if N == 0 then nil else {LeastBitOf N}|{ReversedBinaryDigitsOf N div 2} end end RevPatterns3 = {Reverse [1#1#1 1#1#0 1#0#1 1#0#0 0#1#1 0#1#0 0#0#1 0#0#0]} fun {SelectPatterns RevPats RevBits} case RevPats#RevBits of (P|Ps)#(B|Bs) then if B == 1 then P|{SelectPatterns Ps Bs} else {SelectPatterns Ps Bs} end else nil end end fun {Rule N} %% REQUIRES: N < 256 %% ENSURES: Result is a function that takes 3 arguments and computes the Nth rule %% from Wolfram's numbering scheme of rules. RevBits = {ReversedBinaryDigitsOf N} OnePatterns = {SelectPatterns RevPatterns3 RevBits} in fun {$ Left This Right} if {Member Left#This#Right OnePatterns} then 1 else 0 end end end