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