繁体   English   中英

F#和结果类型将是无限错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM