我在这个函数中陷入了无限循环:
let rec showGoatDoorSupport(userChoice, otherGuess, aGame) =
if( (userChoice != otherGuess) && (List.nth aGame otherGuess == "goat") ) then otherGuess
else showGoatDoorSupport(userChoice, (Random.int 3), aGame);;
这是我调用函数的方式:
showGoatDoorSupport(1, 2, ["goat"; "goat"; "car"]);
在函数的第一个条件中,我比较前 2 个输入参数(1 和 2)是否不同,并且如果列表中索引“otherGuess”的项目不等于“goat”,我想返回那个其他猜。
否则,我想使用 0-2 之间的随机数作为第二个输入参数再次运行该函数。
关键是继续尝试运行该函数,直到第二个参数不等于第一个参数,并且列表中的那个槽不是“山羊”,然后返回那个槽号。