Compiler errors F签名理解:->运算符和编译器错误
我不时会遇到如下错误:Compiler errors F签名理解:->运算符和编译器错误,compiler-errors,f#,signature,Compiler Errors,F#,Signature,我不时会遇到如下错误: 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 'i
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个整数并返回字符串的函数。出现这种情况的原因之一是,您还可以将此函数视为一个接受1 int并返回一个接受1 int并返回字符串的函数,这称为部分应用程序
在您的情况下,我会使用错误中的行号来帮助您定位问题。
所以在第28行,你可以给它一个函数,它接受一个int并返回一个int,但是它需要一个int值,也许你忘了用一个输入调用这个函数?
在第33行,它需要int list,这是表示list的另一种方式。然而,您给了它一个函数,它接受一个int、一个list并返回list。同样,为了满足类型约束,可能需要使用两个输入调用此函数
编辑:再看看这个,我想我可以猜出哪些行是错误的。看起来,当您调用其中一些函数时,您正在将多个参数放在括号中。
请尝试将您的代码更新为:
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 []
例如,要调用具有签名字符串->int->int的breakFinder,您可以这样做:让number=breakFinder param1 42读取F函数签名时,箭头->是一个分隔符,您可以读取以下签名:
int -> int -> string
例如,作为接受2个整数并返回字符串的函数。出现这种情况的原因之一是,您还可以将此函数视为一个接受1 int并返回一个接受1 int并返回字符串的函数,这称为部分应用程序
在您的情况下,我会使用错误中的行号来帮助您定位问题。
所以在第28行,你可以给它一个函数,它接受一个int并返回一个int,但是它需要一个int值,也许你忘了用一个输入调用这个函数?
在第33行,它需要int list,这是表示list的另一种方式。然而,您给了它一个函数,它接受一个int、一个list并返回list。同样,为了满足类型约束,可能需要使用两个输入调用此函数
编辑:再看看这个,我想我可以猜出哪些行是错误的。看起来,当您调用其中一些函数时,您正在将多个参数放在括号中。
请尝试将您的代码更新为:
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 []
例如,要调用具有签名字符串->int->int的breakFinder,您可以这样做:如果添加行号,或者在与错误匹配的行旁边添加注释,则让number=breakFinder param1 42,这将有助于我们识别此处的问题。我在代码中添加了注释。它在每一个递归调用中都是errorsThanks,这符合我的假设。请尝试我的答案,并让我知道它是否有帮助。如果您添加行号,或者在与错误匹配的行旁边添加注释,这将有助于我们确定此处的问题。我在代码中添加了注释。它在每一个递归调用中都是errorsThanks,这符合我的假设。请尝试我的答案,让我知道它是否有帮助。这修复了它谢谢!我现在明白问题了!你是最棒的!这个修好了,谢谢!我现在明白问题了!你是最棒的!