【发布时间】:2015-06-04 00:38:50
【问题描述】:
如果没有 else 语句是否可以嵌套。我编写了以下无用的程序来演示嵌套 if。我该如何解决这个问题,使其在语法方面是正确的。第 5 行和第 6 行给出了错误。
let rec move_helper b sz r = match b with
[] -> r
|(h :: t) ->
if h = 0 then
if h - 1 = sz then h - 1 ::r
if h + 1 = sz then h + 1 ::r
else move_helper t sz r
;;
let move_pos b =
move_helper b 3 r
;;
let g = move_pos [0;8;7;6;5;4;3;2;1]
【问题讨论】:
-
如果你没有演示嵌套 ifs,你可以匹配 (h, h-sz) - 在实际代码中,不要使用嵌套 ifs 作为模式匹配或相互递归函数做很多 更好地构建程序。