F# F“解决方案”;逃离扎尔格“;令人费解的事

F# F“解决方案”;逃离扎尔格“;令人费解的事,f#,F#,我已经写了一个F#程序来解决这个“难题” 我的代码如下。但不知何故,当谜题解决时,我返回布尔值的方式出了问题 在线 retVal = Move (cost + (MoveCost toy1 toy2)) Right remainingElements 我得到一个警告 表达式的类型应为“unit”,但类型为“bool”。如果分配属性使用语法“obj.Prop,则无法重新分配let绑定。应该是: let mutable retVal = false 编辑:我发布了一个,如果您需要一个参考实现。

我已经写了一个F#程序来解决这个“难题”

我的代码如下。但不知何故,当谜题解决时,我返回布尔值的方式出了问题

在线

retVal = Move (cost + (MoveCost toy1 toy2)) Right remainingElements
我得到一个警告


表达式的类型应为“unit”,但类型为“bool”。如果分配属性使用语法“obj.Prop,则无法重新分配
let
绑定。应该是:

let mutable retVal = false


编辑:我发布了一个,如果您需要一个参考实现。

您不能重新分配
let
绑定。应该是:

let mutable retVal = false


编辑:如果您需要一个参考实现,我发布了一个。

请注意,您的
MoveCost
实现可以更简洁有效地实现为
let inline MoveCost{Cost=cost1}{Cost=cost2}=max cost1 cost2
。请注意,您的
MoveCost
实现可以更简洁高效地实现为
let inline MoveCost{Cost=cost1}{Cost=cost2}=max cost1 cost2
retVal <- Move (cost + (MoveCost toy1 toy2)) Right remainingElements
let res =
  [
    for i in group do 
      for j in group do 
        if i < j then yield i, j elif i > j then yield j, i
  ]
  |> List.filter (fun (toy1, toy2) ->
    let remainingElements = List.filter (fun t-> t.Name <> toy1.Name && t.Name <> toy2.Name) group                
    Move (cost + (MoveCost toy1 toy2)) Right remainingElements)

match res with
| [] -> false
| _ ->
  res |> List.iter (fun (toy1, toy2) ->
    Console.WriteLine ("Move " + toy1.Name + " and " + toy2.Name + " with the total cost of " + cost.ToString()))
  true