Stadsbjerg
Stadsbjerg

Reputation: 33

Incomplete structured construct at or before this point in pattern matching

So, I'm doing an assignment where I'm supposed to write a function that prints an image based on some a type, figure, which I have defined.

I'm to use this library and it's functions with my own .fsx file, which should then be compiled and able to create images, based on a descriptive "figure"-type ; https://github.com/diku-dk/img-util-fs

My .fsx code looks like this;

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)

But when I try to compile the library and my .fsx file I get the error "8io.fsx(44.13): error FS0010: Incomplete structured construct at or before this point in pattern matching. Expected '->' or other token"

I'm fairly new to coding, and my code might not be usable, but it's the only compiling error I get, so I hope it's salvageable

Upvotes: 2

Views: 361

Answers (1)

Brian Berns
Brian Berns

Reputation: 17038

I think the problem you're asking about is caused by this construct:

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

The problem here is that colorAt n x appears unexpectedly between the with and the first |. A typical match expression should look more like this:

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

Note: I haven't examined your code for correctness. This only addresses the specific syntactical issue identified by the compiler.

Upvotes: 1

Related Questions