克服名单不变性?

时间:2014-03-22 03:32:31

标签: ocaml tic-tac-toe

let board =[0;0;0;0;0;0;0;0;0];;

我正在尝试使用我编写的某些函数的返回值来更改board的值。 这将是我的游戏循环:

let rec f gamestate = match gamestate with
9 -> ()
| _ ->
let _ = print_board board in
let _ = print_string "\n" in
let _ = print_string "row: " in
let row = read_int () in
let _ = print_string "col: " in
let col = read_int () in
board = (player_move board row col);
(*trying to change the value of board to (player_move board row col)*)

f (gamestate + 1);;
f 0;;

我确保所有功能都按预期执行。 我能够通过以下方式这样做:

let _ = print_board (player_move board row col) in

这给了我预期的输出但是我没有办法保存"新修改的board

我是否必须使用Object

1 个答案:

答案 0 :(得分:1)

基本技巧是使board成为f的参数。然后,您可以使用新主板以递归方式调用f