Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. type cell = { alive : bool ; column : int ; row : int }
  2. ;;
  3. type cellzipper = cell list * cell * cell list
  4. ;;
  5. type grid = {gamegrid : cell list list}
  6. ;;
  7.  
  8. type gridzipper =
  9. { above : grid
  10. ; below : grid
  11. ; left : cell list
  12. ; right : cell list
  13. ; focus : cell }
  14.  
  15. let focuscell celllist n =
  16. let rec loop acc n l =
  17. match l,n with
  18. | hd :: tl,n when n > 0 -> loop (hd :: acc) (n - 1) tl
  19. | [],_ -> None
  20. | hd :: tl,0 -> Some (acc, hd, tl)
  21. in loop [] 0 celllist
  22. ;;
  23.  
  24. let gridfocus x y g =
  25. let a = focuscell g x in
  26. match a with
  27. | Some(before, line , after) -> (
  28. let b = focuscell line y in
  29. match b with
  30. Some (left , focus, right) ->
  31. let above = { gamegrid = before } in
  32. let below = { gamegrid = after} in
  33. Some(
  34. { above
  35. ; below
  36. ; left
  37. ; right
  38. ; focus }
  39. )
  40. | None -> None
  41. )
  42. | None -> None
  43. ;;
  44.  
  45. let left g =
  46. match g.left with
  47. [] -> None
  48. | hd::tl -> let newgridzipper = { g with focus = hd; left = tl; right = g.right @ [g.focus] } in
  49. Some(newgridzipper)
  50. ;;
  51.  
  52. let right g =
  53. match g.left with
  54. [] -> None
  55. | hd::tl -> let newgridzipper = { g with focus = hd; left = [g.focus]; right = tl } in
  56. Some(newgridzipper)
  57. ;;
  58.  
  59. (*pattern-matches on the list (of lists) , which should be non-empty, and introduces two bindings,
  60. line for the head, and above for the tail.*)
  61. let up g =
  62. match g.above,g.below with
  63. | {gamegrid = line :: above},{gamegrid = below} -> (
  64. let below' = (List.rev g.left) :: ([g.focus] @ g.right) :: below in
  65. let a = focuscell line (List.length g.left) in
  66. match a with
  67. Some (left, focus, right) ->
  68. let above = { gamegrid = above } in
  69. let below = { gamegrid = below'} in
  70. { above
  71. ; below
  72. ; left
  73. ; right
  74. ; focus }
  75. |None ->
  76. { above = g.above
  77. ; below = g.below
  78. ; left = g.left
  79. ; right = g.right
  80. ; focus = g.focus }
  81. )
  82. |({gamegrid=[]},_) -> { above = g.above
  83. ; below = g.below
  84. ; left = g.left
  85. ; right = g.right
  86. ; focus = g.focus }
  87. ;;
  88.  
  89. let down g =
  90. match g.below,g.above with
  91. | {gamegrid = line :: below},{gamegrid = above} -> (
  92. let above' = (List.rev g.left) :: ([g.focus] @ g.right) :: above in
  93. let a = focuscell line (List.length g.left) in
  94. match a with
  95. Some (left, focus, right) ->
  96. let above = { gamegrid = above } in
  97. let below = { gamegrid = above'} in
  98. { above
  99. ; below
  100. ; left
  101. ; right
  102. ; focus }
  103. |None ->
  104. { above = g.above
  105. ; below = g.below
  106. ; left = g.left
  107. ; right = g.right
  108. ; focus = g.focus }
  109. )
  110. | ({gamegrid=[]},_)-> { above = g.above
  111. ; below = g.below
  112. ; left = g.left
  113. ; right = g.right
  114. ; focus = g.focus }
  115.  
  116. ;;
  117.  
  118. #require "containers";;
  119. let makegrid = CCList.init 2 ( fun i -> (CCList.init 2 (fun j -> { alive = true; column = j;row = i })) );;
  120.  
  121. let grid = makegrid in
  122. match grid with
  123. | Some(gridzipper) ->
  124. left gridzipper;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement