类型不匹配 Seq.unfold

swa*_*eel 1 f# f#-interactive

let fibgen (x,y) = if(x < 4000000) then Some(x+y, (y, x+y)) else None

let fibseq = Seq.unfold fibgen (1,1)
Run Code Online (Sandbox Code Playgroud)

在第二行键入不匹配错误。我究竟做错了什么?我正在使用 F# 2.0

回答

首先在我的交互式窗口上完成重置会话仍然出现相同的错误
,然后重新启动我的项目现在工作正常。

是的,我在同一会话中执行了下面的代码

// The option type is a discriminated union.
type Option<'a> =
    | Some of 'a
    | None




Interactive window Output as below

val fibgen : int * int -> Option<int * (int * int)>

> 


Eluer2.fs(27,25): error FS0001: Type mismatch. Expecting a
    int * int -> ('a * (int * int)) option    
but given a
    int * int -> Option<int * (int * int)>    
The type '('a * (int * int)) option' does not match the type 'Option<int * (int * int)>'
> 
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 5

正如 Carsten 在评论中指出的那样,如果您在干净的 F# Interactive 中输入代码,则代码可以正常工作。由于编译器抱怨Option<'T>does not match 'T option,我猜测您在使用该函数之前Some不小心编写了一些重新定义的代码。也许您写了这样的东西(并在 F# Interactive 中对其进行了评估):Noneunfold

type Option<'T> = 
  | None
  | Some of 'T
Run Code Online (Sandbox Code Playgroud)

Some这隐藏了和 的标准定义,None它们是'T option标准 F# 库中定义的类型的构造函数(并且是unfold函数所期望的)。

您可以通过在窗口中右键单击并选择“重置会话”来重置 F# Interactive 会话。这会删除以前的声明,并且代码应该可以正常工作。