compiler-errors - F#签名了解: “the -> operator and Compiler Errors”

标签 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
    '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>的另一种方式。但是,您为它提供了一个函数,该函数接受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 -> p@[(breakFinder stringPixel 0)]
                            |_ -> yello 0 (y+1) (p@[(breakFinder stringPixel 0)])
      |_ -> yello (x+1)(y)(p@[(breakFinder stringPixel 0)])
    yello 0 0 []

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

关于compiler-errors - F#签名了解: “the -> operator and Compiler Errors” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790066/

相关文章:

.net - 编译器失败,错误代码为 -1073741502(SharePoint 2010 SP1 在 Windows Server 2008 R2 上运行)

f# - f# 中具有属性分配的多个构造函数

pdf - 在包含附件的 PDF 上签名

xcode - 强制所有错误显示在 Xcode 中

spring - 如何在IntelliJ IDEA中应用@ControllerAdvice?

.net - 签名中类型前面的 # 符号 (F#) 有何含义?

sml - 标准 ML : making a type transparent with opaque signature ascription

node.js - 无法使用 nodejs 加密签署文件

java - 编译 Java 程序后,出现错误 : incompatible types: double cannot be converted to double[]

.net-core - 步长大于 1 的数组并行迭代