Why these work
let x = seq { for i in 1 .. 10 do yield i }
let x = seq { for i in 1 .. 10 -> i }
let x = seq { for i = 1 to 10 do yield i }
but this one doesn't?
let x = seq { for i = 1 to 10 -> i }
According to the F# specification, sequence expression can be either normal computation expression (this is the case where you write do yield
) or it can be a short form that is specific to sequence expressions:
seq { comp-expr }
seq { short-comp-expr }
The comp-expr
case covers your first and last working examples. The short form uses ->
and the specification explicitly says that the only allowed short form is with the in
keyword:
short-comp-expr :=
for pat in expr-or-range-expr -> expr -- yield result
There are many other short forms that would be useful in practice, but I guess that the aim is to provide a special syntax just for this one, very frequent, case and otherwise keep the language uniform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With