2017-01-22 34 views
1

不时我得到看起来像这样的错误:F#签名理解:“ - >运算符和编译器错误”

opgave.fsx(28,14): error FS0001: This expression was expected to have type 
    'int' 
but here has type 
    'int -> int' 

opgave.fsx(33,35): error FS0001: This expression was expected to have type 
    'int list' 
but here has type 
    'int -> int list -> int list' 

什么混淆我是什么呢由->运营商意味着什么?据我了解,然后从第一个错误,然后预计一个int,但是给出一个表达式,接受一个int并返回另一个int。也许我误解了?如果我是正确的,那究竟是什么问题?我可以发誓我之前做过类似的事情。

代码这些错误都是基于这个样子的:

member this.getPixelColors(x,y,p) : int list = 
    let pixel = image.GetPixel(x,y) 
    let stringPixel = pixel.ToString() 
    let rec breakFinder (s:string) (h:int) = 
     match s.[h] with 
     |',' -> s.[9..(h-1)] |> int 
     |_ -> (breakFinder(s (h+1))) // this is line 28 
    let rec yello x y p = 
     match x with 
     |l when l = imageW -> match y with 
          |k when k = imageH -> [email protected][(breakFinder stringPixel 0)] 
          |_ -> yello((0)(y+1)([email protected][(breakFinder stringPixel 0)])) // this is line 33 
     |_ -> yello((x+1)(y)([email protected][(breakFinder stringPixel 0)])) // there is an error in this line aswell identical to line 33 
    yello 0 0 [] 

有人可以让我明白,所以我将能够处理这个对我自己的未来?

+2

如果添加行号,或也许把旁边的意见符合错误的行,这将帮助我们识别这里的问题。 – Stuart

+2

我在代码中添加了评论。这是在每个递归调用它的错误 – Nulle

+1

谢谢,这符合我的假设。请尝试我的答案,并告诉我是否有帮助。 – Stuart

回答

4

当读取F#函数签名时,箭头(->)是分离器,并且可以读取后续签名:

int -> int -> string 

例如作为需要2个int S和返回string的功能。其中一个原因是因为您也可以将此函数想象为一个函数,它需要1 int并返回一个函数,该函数需要1 int并返回string,这称为部分应用程序。

在你的情况下,我会使用错误中的行号来帮助你指出问题。 因此,在第28行,你可以给它一个函数,它需要一个int并返回一个int,但它想要一个int值,也许你忘记调用带有输入的函数? 在第33行,它需要int list这是另一种表达list<int>的方式。然而,你给它的功能需要intlist<int>并返回list<int>。同样,也许你需要用两个输入来调用这个函数,以便满足你的类型约束。

编辑:再看这个,我想我可以猜测哪些行是错误的。它看起来像当你调用其中一些函数时,你将多个参数放在圆括号中。 尝试更新的代码如下:

member this.getPixelColors(x,y,p) : int list = 
    let pixel = image.GetPixel(x,y) 
    let stringPixel = pixel.ToString() 
    let rec breakFinder (s:string) (h:int) = 
     match s.[h] with 
     |',' -> s.[9..(h-1)] |> int 
     |_ -> (breakFinder s (h+1)) 
    let rec yello x y p = 
     match x with 
     |l when l = imageW -> match y with 
          |k when k = imageH -> [email protected][(breakFinder stringPixel 0)] 
          |_ -> yello 0 (y+1) ([email protected][(breakFinder stringPixel 0)]) 
     |_ -> yello (x+1)(y)([email protected][(breakFinder stringPixel 0)]) 
    yello 0 0 [] 

例如,调用breakFinder具有签名string -> int -> int,你可以这样做:let number = breakFinder "param1" 42

+1

这解决了它,谢谢!我现在看到问题了!你是最棒的! – Nulle