Jos*_*eau 5 ocaml functional-programming
我在这个函数中陷入了无限循环:
let rec showGoatDoorSupport(userChoice, otherGuess, aGame) =
if( (userChoice != otherGuess) && (List.nth aGame otherGuess == "goat") ) then otherGuess
else showGoatDoorSupport(userChoice, (Random.int 3), aGame);;
Run Code Online (Sandbox Code Playgroud)
以下是我调用函数的方法:
showGoatDoorSupport(1, 2, ["goat"; "goat"; "car"]);
Run Code Online (Sandbox Code Playgroud)
在函数的第一个条件中,我比较前两个输入参数(1和2),如果它们不同,如果列表中索引"otherGuess"的项不等于"goat",我想返回otherGuess.
否则,我想再次使用0-2之间的随机数作为第二个输入参数运行该函数.
关键是继续尝试运行该函数,直到第二个参数不等于第一个,并且List中的那个插槽不是"山羊",然后返回该插槽号.
不要使用==,它检查物理平等.使用=.两个不同的字符串永远不会在物理上相等,即使它们包含相同的字符序列.(这是必要的,因为字符串在OCaml中是可变的.)
$ ocaml
OCaml version 4.00.0
# "abc" == "abc";;
- : bool = false
# "abc" = "abc";;
- : bool = true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
404 次 |
| 最近记录: |