Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type cell = { alive : bool ; column : int ; row : int }
- ;;
- type cellzipper = cell list * cell * cell list
- ;;
- type grid = {gamegrid : cell list list}
- ;;
- type gridzipper =
- { above : grid
- ; below : grid
- ; left : cell list
- ; right : cell list
- ; focus : cell }
- let focuscell celllist n =
- let rec loop acc n l =
- match l,n with
- | hd :: tl,n when n > 0 -> loop (hd :: acc) (n - 1) tl
- | [],_ -> None
- | hd :: tl,0 -> Some (acc, hd, tl)
- in loop [] 0 celllist
- ;;
- let gridfocus x y g =
- let a = focuscell g x in
- match a with
- | Some(before, line , after) -> (
- let b = focuscell line y in
- match b with
- Some (left , focus, right) ->
- let above = { gamegrid = before } in
- let below = { gamegrid = after} in
- Some(
- { above
- ; below
- ; left
- ; right
- ; focus }
- )
- | None -> None
- )
- | None -> None
- ;;
- let left g =
- match g.left with
- [] -> None
- | hd::tl -> let newgridzipper = { g with focus = hd; left = tl; right = g.right @ [g.focus] } in
- Some(newgridzipper)
- ;;
- let right g =
- match g.left with
- [] -> None
- | hd::tl -> let newgridzipper = { g with focus = hd; left = [g.focus]; right = tl } in
- Some(newgridzipper)
- ;;
- (*pattern-matches on the list (of lists) , which should be non-empty, and introduces two bindings,
- line for the head, and above for the tail.*)
- let up g =
- match g.above,g.below with
- | {gamegrid = line :: above},{gamegrid = below} -> (
- let below' = (List.rev g.left) :: ([g.focus] @ g.right) :: below in
- let a = focuscell line (List.length g.left) in
- match a with
- Some (left, focus, right) ->
- let above = { gamegrid = above } in
- let below = { gamegrid = below'} in
- { above
- ; below
- ; left
- ; right
- ; focus }
- |None ->
- { above = g.above
- ; below = g.below
- ; left = g.left
- ; right = g.right
- ; focus = g.focus }
- )
- |({gamegrid=[]},_) -> { above = g.above
- ; below = g.below
- ; left = g.left
- ; right = g.right
- ; focus = g.focus }
- ;;
- let down g =
- match g.below,g.above with
- | {gamegrid = line :: below},{gamegrid = above} -> (
- let above' = (List.rev g.left) :: ([g.focus] @ g.right) :: above in
- let a = focuscell line (List.length g.left) in
- match a with
- Some (left, focus, right) ->
- let above = { gamegrid = above } in
- let below = { gamegrid = above'} in
- { above
- ; below
- ; left
- ; right
- ; focus }
- |None ->
- { above = g.above
- ; below = g.below
- ; left = g.left
- ; right = g.right
- ; focus = g.focus }
- )
- | ({gamegrid=[]},_)-> { above = g.above
- ; below = g.below
- ; left = g.left
- ; right = g.right
- ; focus = g.focus }
- ;;
- #require "containers";;
- let makegrid = CCList.init 2 ( fun i -> (CCList.init 2 (fun j -> { alive = true; column = j;row = i })) );;
- let grid = makegrid in
- match grid with
- | Some(gridzipper) ->
- left gridzipper;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement