- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
资深 C# 开发人员,正在学习 F#。我选择了一段相当实用的 C# 代码,我将其作为学习练习编写 - 将其转换为 F#。我阅读了大量有关函数式编程的书籍,并定期使用 C# 中的函数结构,但我只花了几个小时就开始学习 F#。
此函数是解决类似于“LonPos 101”谜题的程序的一部分,您可以在亚马逊等网站上找到该程序。求解器中使用的策略是基于识别谜题空间中只有 30 个有效位置,因此“到目前为止的解决方案”可以用一个整数表示,并且每件作品的每个有效位置也可以用一个整数表示,并且完整的解决方案是一个包含 7 件作品中每件作品的一个可能位置的集合,其中7 个部分的“重量”加起来就是解决方案的重量 (2^30-1)。在给定的函数中,“key”是棋子的“主键”,wbk 是“键的权重”——由键索引,包含相应棋子的有效位置列表,而“w”是前面提到的“解决方案” -至今”。返回值是从键到所选位置的映射,并填充在成功递归导致解决方案的退出路径上。我在开发 C# 解决方案时发现,将其设为排序列表会使解决方案查找器的速度提高一个数量级,但它对于普通列表同样有效。
这是我遇到问题的 C# 函数:
int solutionWeight;
Dictionary<int,int> Evaluate(int w, Dictionary<int, SortedSet<int>> wbk, int key)
{
if (w == solutionWeight)
return new Dictionary<int, int>();
if (key == 8)
return null;
foreach (var w2 in wbk[key])
{
if ((w & w2) != 0)
continue;
var s = Evaluate(w | w2, wbk, key + 1);
if (s != null)
{
s.Add(key, w2);
return s;
}
}
return null;
}
这是我对 F# 版本的尝试 - 它可以编译,但不能正常工作 - 当执行 w 不是 solutionWeight 且 key 是等于 8。我不明白为什么在这种情况下甚至要执行这行代码,但是...
let rec Evaluate(w:int, wbk:Dictionary<int, SortedSet<int>>, key:int):Dictionary<int,int> =
if w = solutionWeight then
Dictionary<int,int>()
else if key = 8 then
null
else
// ... this is wrong - runs off the end of some collection - fails with key not found exception
let ws = wbk.[key] |> Seq.filter (fun w2 -> (w2 &&& w) = 0)
/// ... for some reason, execution resumes here after the key = 8 clause above
let ss = ws |> Seq.map (fun w -> (w,Evaluate(w, wbk, key+1)))
let sw = ss |> Seq.find (fun sw -> snd sw <> null)
let s = snd sw
s.Add(key, fst sw)
s
给我指明正确的方向!我的下一次尝试将首先以更函数式的风格重写 C# 代码,但感觉这个版本即将运行(虽然可能离惯用的 F# 还很远)。
更新:
我重新编写了 F# 函数,通过使用一对相互递归的函数来消除循环。它确实有效,但比非相互递归的 C# 版本慢 ~2 倍。
let rec Evaluate(w:int, wbk:Dictionary<int, SortedSet<int>>, key:int):Dictionary<int,int> =
if w = solutionWeight then
Dictionary<int,int>()
else if key = 8 then
null
else
EvalHelper(w, wbk, key, wbk.[key].GetEnumerator())
and EvalHelper(w:int, wbk:Dictionary<int, SortedSet<int>>, key:int, ws:IEnumerator<int>):Dictionary<int,int> =
if ws.MoveNext() then
let w2 = ws.Current
if (w &&& w2) = 0 then
let s = Evaluate(w ||| w2, wbk, key+ 1)
if s <> null then
s.Add(key, w2)
s
else
EvalHelper(w, wbk, key, ws)
else
EvalHelper(w, wbk, key, ws)
else
null
我怎样才能进一步改进它?
更新:我对它进行了更多调整 - 感觉有点像 F#,但我仍然觉得我应该能够摆脱更多类型注释并更多地使用 F# native 类型。这是一项正在进行的工作。
let rec Evaluate(w, wbk:Dictionary<int, SortedSet<int>>, key):Dictionary<int,int> option =
let rec EvalHelper(ws) =
match ws with
| w2 :: mws ->
match w &&& w2 with
| 0 ->
let s = Evaluate(w ||| w2, wbk, key+ 1)
match s with
| None -> EvalHelper(mws)
| Some s ->
s.Add(key, w2)
Some(s)
| _ -> EvalHelper(mws)
| _ ->
None
if w = solutionWeight then
Some (Dictionary<int,int>())
else if key = 8 then
None
else
EvalHelper(List.ofSeq wbk.[key])
最佳答案
翻译这个函数的关键是把 for 循环变成递归,如第一次更新所示。
let rec Evaluate(w:int, wbk:Dictionary<int, SortedSet<int>>, key:int):Dictionary<int,int> =
if w = solutionWeight then
Dictionary<int,int>()
else if key = 8 then
null
else
EvalHelper(w, wbk, key, wbk.[key].GetEnumerator())
and EvalHelper(w:int, wbk:Dictionary<int, SortedSet<int>>, key:int, ws:IEnumerator<int>):Dictionary<int,int> =
if ws.MoveNext() then
let w2 = ws.Current
if (w &&& w2) = 0 then
let s = Evaluate(w ||| w2, wbk, key+ 1)
if s <> null then
s.Add(key, w2)
s
else
EvalHelper(w, wbk, key, ws)
else
EvalHelper(w, wbk, key, ws)
else
null
随后的更新只是稍微清理了样式 - 使其更接近惯用的 F#。
let rec Evaluate(w, wbk:Dictionary<int, SortedSet<int>>, key):Dictionary<int,int> option =
let rec EvalHelper(ws) =
match ws with
| w2 :: mws ->
match w &&& w2 with
| 0 ->
let s = Evaluate(w ||| w2, wbk, key+ 1)
match s with
| None -> EvalHelper(mws)
| Some s ->
s.Add(key, w2)
Some(s)
| _ -> EvalHelper(mws)
| _ ->
None
if w = solutionWeight then
Some (Dictionary<int,int>())
else if key = 8 then
None
else
EvalHelper(List.ofSeq wbk.[key])
关于c# - 递归 C# 函数从 for 循环内部返回 - 如何转换为 F#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304923/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!