有时我会收到如下所示的错误:
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 -> p@[(breakFinder stringPixel 0)]
|_ -> yello((0)(y+1)(p@[(breakFinder stringPixel 0)])) // this is line 33
|_ -> yello((x+1)(y)(p@[(breakFinder stringPixel 0)])) // there is an error in this line aswell identical to line 33
yello 0 0 []
有没有人能让我明白,这样我以后就可以自己处理这件事了?
发布于 2017-01-22 11:36:36
在读取F#函数签名时,箭头(->
)是分隔符,您可以读取以下签名:
int -> int -> string
例如,作为一个接受2个int
s并返回一个string
的函数。它这样呈现的原因之一是因为你也可以把这个函数想象成一个函数,它接受1个int
,然后返回一个函数,这个函数接受1个int
,然后返回一个string
,这就是所谓的部分应用。
在您的情况下,我将使用错误中的行号来帮助您指出问题。在第28行,您可以给它一个函数,该函数接受一个int
并返回一个int
,但它需要一个int
值,也许您忘记了使用输入调用该函数?在第33行,它需要int list
,这是另一种表达list<int>
的方式。但是,您为它提供了一个函数,该函数接受一个int
、一个list<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 -> p@[(breakFinder stringPixel 0)]
|_ -> yello 0 (y+1) (p@[(breakFinder stringPixel 0)])
|_ -> yello (x+1)(y)(p@[(breakFinder stringPixel 0)])
yello 0 0 []
例如,要调用具有签名string -> int -> int
的breakFinder
,您可以这样做:let number = breakFinder "param1" 42
https://stackoverflow.com/questions/41790066
复制