有时我会收到如下错误:
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 []
有人可以让我理解,以便将来我可以自己处理吗?
最佳答案
读取F#函数签名时,箭头(->
)是分隔符,您可以阅读以下签名:
int -> int -> string
例如,作为一个函数,它需要2个
int
并返回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 []
例如,要调用具有签名
breakFinder
的string -> int -> int
,您可以这样做:let number = breakFinder "param1" 42
关于compiler-errors - F#签名了解: “the -> operator and Compiler Errors” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790066/