[英]F# and resulting type would be infinite error
我有以下递归函数:
let rec ComputePath v items =
match v with
| x when x <> source -> ComputePath edgeTo.[x] x::items
| s when s = source -> s::items
| _ -> items
ComputePath v [] |> Some
在我调用递归函数的第三行中,我收到以下错误:类型不匹配。 统一“和列表”时,结果类型将是无限的。
如果我通过将方法参数转换为元组来更改方法参数,那么错误就会消失。
let rec ComputePath (v, items) =
match v with
| x when x <> source -> ComputePath (edgeTo.[x], x::items)
| s when s = source -> s::items
| _ -> items
ComputePath (v, []) |> Some
edgeTo是一个整数数组。 我需要在原始函数定义中进行哪些更改才能使其正常工作?
你需要在x::items
周围添加一些(...)
记住 :函数应用程序具有以前最高的:
match v with
| x when x <> source -> ComputePath edgeTo.[x] (x::items)
....
没有它你会得到这个:
(ComputePath edgeTo.[x] x) :: items
并且错误消息很有意义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.