Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do yield in for loop within sequence computation expression

Tags:

sequence

f#

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 }
like image 987
Panos Avatar asked Apr 18 '12 09:04

Panos


1 Answers

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.

like image 72
Tomas Petricek Avatar answered Sep 24 '22 16:09

Tomas Petricek