4
let compareDiagonal p x y =
    System.Math.Abs((int)(x - (fst p))) <> System.Math.Abs((int)(y  - (snd p)));;

let isAllowed p = function
    | [] -> true
    | list -> List.forall (fun (x, y) -> fst p <> x && snd p <> y && (compareDiagonal p x y))  list;;

let rec solve col list =
    let solCount : int = 0
    match col with
    | col when col < 8 ->
        for row in [0 .. 7] do
            solCount = solCount + if isAllowed (row, col) list then solve (col + 1) ((row, col) :: list) else 0
        solCount        
    | _ -> 1;;

let solCount = solve 0 [];;
solCount;;

我收到错误

 solCount = solCount + if isAllowed (row, col) list then (solve (col + 1) ((row, col) :: list)) else 0
------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

stdin(335,13): warning FS0020: This expression should have type 'unit', but has type 'bool'. If assigning to a property use the syntax 'obj.Prop <- expr'.

为什么我无法返回号码?

4

1 回答 1

4

有两个相关的问题。

默认情况下,F# 中的变量是不可变的。如果你想要一个可变变量,你必须声明它,像这样:

let mutable solCount : int = 0

然后,当您为其分配值而不是使用时,=您必须<-像这样使用:

solCount <- solCount + if isAllowed (row, col) list then solve (col + 1) ((row, col) :: list) else 0

接下来是一个完整的例子。

但是,这不是执行此类操作的正确功能方式。不要使用循环来累加值,而是使用递归函数随时返回累积值。使用 F# 函数式程序的设计方式几乎总能产生更好的结果,尽管需要一些时间来适应。

您的原始示例是可变的,而不是“功能方式”:

let compareDiagonal p x y =
    System.Math.Abs((int)(x - (fst p))) <> System.Math.Abs((int)(y  - (snd p)));;

let isAllowed p = function
    | [] -> true
    | list -> List.forall (fun (x, y) -> fst p <> x && snd p <> y && (compareDiagonal p x y))  list;;

let rec solve col list =
    let mutable solCount : int = 0
    match col with
    | col when col < 8 ->
        for row in [0 .. 7] do
            solCount <- solCount + if isAllowed (row, col) list then solve (col + 1) ((row, col) :: list) else 0
        solCount        
    | _ -> 1;;

let solCount = solve 0 [];;
solCount;;
于 2013-10-13T04:20:42.843 回答