7
votes

In F# the following statement will fail with the following errors

let listx2 = [1..10]
    |> List.map(fun x -> x * 2)
    |> List.iter (fun x -> printf "%d " x)

Block following this 'let' is unfinished. Expect an expression.

Unexpected infix operator in binding. Expected incomplete structured construct at or before this point or other token.

However the following will compile

let listx2 = 
    [1..10]
    |> List.map(fun x -> x * 2)
    |> List.iter (fun x -> printf "%d " x)

I also noticed that this compiles but has a warning

let listx2 = [1..10] |>
    List.map(fun x -> x * 2) |>
    List.iter (fun x -> printf "%d " x)

Possible incorrect indentation: this token is offside of context started at position (10:18). Try indenting this token further or using standard formatting conventions.

What is the difference between the first two statements?

1

1 Answers

7
votes

When you have

 let listx2 = [1..10]

you are implicitly setting the indent level of the next line to be at the same character as the [. As given by the following rule for offside characters from the spec:

Immediately after an = token is encountered in a Let or Member context.

So in the first example, the |> is indented less than [ so you get an error, but in the second they are the same, so it all works.

I am not quite sure why moving the |> to the end of the line only gives a warning though.