【问题标题】:Incomplete structured construct at or before this point in pattern matching模式匹配中在此点或之前的不完整结构化构造
【发布时间】:2022-01-04 13:35:41
【问题描述】:

所以,我正在做一个任务,我应该编写一个函数,该函数根据我定义的某种类型、图形打印图像。

我将使用这个库,它的功能与我自己的 .fsx 文件一起使用,然后应该根据描述性“figure”-type 对其进行编译并能够创建图像; https://github.com/diku-dk/img-util-fs

我的 .fsx 代码如下所示;

type point = int * int

type color = ImgUtil.color

type figure =
    | Circle of point * int * color
    | Rectangle of point * point * color
    | Mix of figure * figure



let rec colorAt (x,y) figure =
    match figure with
        | Circle ((cx,cy), r, col) ->
            if (x-cx)*(x-cx)+(y-cy)*(y-cy) <= r*r
            then Some col else None
        | Rectangle ((x0,y0), (x1,y1), col) ->
            if x0 <=x && x <= x1 && y0 <= y && y <= y1
            then Some col else None
        | Mix (f1, f2) ->
            match (colorAt (x,y) f1, colorAt (x,y) f2) with
                |(None , c) -> c
                |(c, None) -> c
                |(Some c1, Some c2) ->
                    let (a1 ,r1 ,g1 ,b1) = ImgUtil.fromColor c1
                    let (a2 ,r2 ,g2 ,b2) = ImgUtil.fromColor c2
                    in Some(ImgUtil.fromArgb ((a1+a2)/2, (r1+r2)/2,
                                              (g1+g2)/2, (b1+b2)/2))



let figTest : figure =
    Circle ((50,50), 45, ImgUtil.fromRgb(255,0,0))
    Rectangle ((40,40), (90,110) ImgUtil.fromRgb (0,0,255))



let makePicture (name:string * x:figure * b:int * h:int) : unit =
    let C = ImgUtil.mk b h
    for i = 0 to h
        do ImgUtil.setLine (ImgUtil.fromRgb(128,128,128)) (0,i) (b,i) C
    for n = (0,0) to (b-1,h-1) do
        match ImgUtil.getPixel n C with colorAt n x
            | None -> (128,128,128)
            | Some col -> colorAt n x
do imgUtil.toPngFile ("%A" name) C

do makePicture ("figTest" figTest 101 151)

但是当我尝试编译库和我的 .fsx 文件时,我得到了错误 “8io.fsx(44.13): 错误 FS0010: 模式匹配中此点或之前的不完整结构化构造。预期的 '->' 或其他标记”

我对编码还很陌生,我的代码可能无法使用,但这是我得到的唯一编译错误,所以我希望它是可以挽救的

【问题讨论】:

  • 这里有很多编译错误,即使你修复了第一个。通常,如果您不断监视编辑器提供的错误,代码就不可能进入这种状态。我建议从头开始。您可以将已有的一小部分代码复制到新代码中,但要确保立即修复编译错误,而不是让它们累积起来。如果您在编辑器中没有立即收到有关编译器错误的反馈,请停止一切​​并首先修复它。

标签: f#


【解决方案1】:

我认为你问的问题是由这个结构引起的:

match ImgUtil.getPixel n C with colorAt n x
    | None -> (128,128,128)
    | Some col -> colorAt n x

这里的问题是colorAt n x 意外出现在with 和第一个| 之间。典型的match 表达式应该更像这样:

match ImgUtil.getPixel n C with
    | None -> (128,128,128)
    | Some col -> colorAt n x

注意:我没有检查您的代码是否正确。这仅解决编译器识别的特定语法问题。

【讨论】:

    猜你喜欢
    • 2021-01-05
    • 2020-03-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2017-06-03
    相关资源
    最近更新 更多