val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours
- val transition : t neighbours -> t -> t
+ val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b neighbours -> 'a
+
+ val iter : ('a -> unit) -> 'a neighbours -> unit
+
+ val transition : t neighbours -> t
val default : t
res.(x).(y) <- c;
(d, res)
-let update (type t) (m : (module Automaton with type t = t)) ((d, cells) as board) =
+let update (type t) (m : (module Automaton with type t = t)) board =
let module M = (val m) in
- ( M.transition (M.map (Fun.const d) M.neighbours) d,
+ ( M.transition (M.map (Fun.const (fst board)) M.neighbours),
Array.init 64 (fun x ->
- Array.init 64 (fun y ->
- M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours) cells.(x).(y))) )
+ Array.init 64 (fun y -> M.transition (M.map (fun (dx, dy) -> get (x + dx) (y + dy) board) M.neighbours)))
+ )
let automata =
[
val map : ('a -> 'b) -> 'a neighbours -> 'b neighbours
- val transition : t neighbours -> t -> t
+ val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b neighbours -> 'a
+
+ val iter : ('a -> unit) -> 'a neighbours -> unit
+
+ val transition : t neighbours -> t
val default : t
type t = Off | On | Dying
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
-let transition l = function
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+ f c;
+ List.iter f cs
+
+let transition (c, cs) =
+ match c with
| Off ->
- let n = List.length (List.filter (( = ) On) l) in
+ let n = List.length (List.filter (( = ) On) cs) in
if n = 2 then On else Off
| On -> Dying
| Dying -> Off
type 'a neighbours = 'a list
-let neighbours = [(-1, 0); (0, -1); (0, 1); (1, 0)]
+let neighbours = [(0, 0); (-1, 0); (0, -1); (0, 1); (1, 0)]
let map = List.map
-let transition l c = List.fold_left ( +. ) c l /. 5.
+let fold_left = List.fold_left
+
+let iter = List.iter
+
+let transition l = List.fold_left ( +. ) 0. l /. 5.
let default = 0.
type t = Dead | Alive
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
-let transition l c =
- let n = List.length (List.filter (( = ) Alive) l) in
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+ f c;
+ List.iter f cs
+
+let transition (c, cs) =
+ let n = List.length (List.filter (( = ) Alive) cs) in
if n < 2 || n > 3 then Dead else if n == 3 then Alive else c
let default = Dead
type t = Sol | Mur
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
-let transition l c =
- let n = List.length (List.filter (( = ) Mur) l) in
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+ f c;
+ List.iter f cs
+
+let transition (c, cs) =
+ let n = List.length (List.filter (( = ) Mur) cs) in
if n < 1 || n > 5 then Sol else if n == 3 then Mur else c
let default = Sol
type t = Empty | Conductor | Head | Tail
-type 'a neighbours = 'a list
+type 'a neighbours = 'a * 'a list
-let neighbours = [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)]
+let neighbours = ((0, 0), [(-1, -1); (-1, 0); (-1, 1); (0, -1); (0, 1); (1, -1); (1, 0); (1, 1)])
-let map = List.map
+let map f (c, cs) = (f c, List.map f cs)
-let transition l = function
+let fold_left f a (c, cs) = List.fold_left f (f a c) cs
+
+let iter f (c, cs) =
+ f c;
+ List.iter f cs
+
+let transition (c, cs) =
+ match c with
| Empty -> Empty
| Conductor ->
- let n = List.length (List.filter (( = ) Head) l) in
+ let n = List.length (List.filter (( = ) Head) cs) in
if n = 1 || n = 2 then Head else Conductor
| Head -> Tail
| Tail -> Conductor