inside a sequence expression you need to use "yield" to return values.

ergo

1
seq{ yield 3; yield 1; yield 4}

would work just fine. Of course it's more common to use this to create (possibly infinite) computed sequences :)

By on 1/21/2009 1:54 PM ()

OK, using yield does appear to solve the problem. But the following also works, but doesn't seem to require the use of yield:

1
let x = seq { for i in [| 4; 3; 1; |] -> i }

Since this same for syntax may be used within [] and [| |] I conclude that there is no implied <i>yield<i> associated with this use of <i>for</i>. Bill

By on 1/21/2009 4:10 PM ()

<quote>OK, using yield does appear to solve the problem. But the following also works, but doesn't seem to require the use of yield: let x = seq { for i in [| 4; 3; 1; |] -> i } Since this same <i>for <i>syntax may be used within [] and [| |] I conclude that there is no implied <i>yield<i> associated with this use of <i>for<i>.   Bill</quote> -> is sugar for yield.

By on 1/21/2009 11:34 PM ()

The language grammar here is rather complex and ambiguous here. Briefly, there are three kinds of 'competing' expressions you can put inside [] or [| |]

  1. literal values
  2. arbitrary code with yields
  3. compact sequence expressions (for ... -> ...)

Many expressions can parse either into category 1 or category 2 (the first is usually preferred, I think).

However, unlike arrays and lists, seq{} only allows #2 and #3, not #1.

See also "Sequence and Computation Expression Syntax Regularization and Simplification" in the release notes

[link:blogs.msdn.com]

for a little more detail.

In any case, if you want a 'seq literal', this should work fine:

[ 1; 2; 3 ] :> seq<_>

By on 1/21/2009 4:39 PM ()

Brian

Thanks for the explanation and reference to the release notes. I'm curious whether the exclusion of category #1 is a feature or a bug though? I reviewed the release notes and didn't see anything that directly addresses this issue. Is there a rationale for this "inconsistency"?

Bill

By on 1/21/2009 7:41 PM ()

It's a feature. I don't think there's a scenario for a 'literal seq' of values; when creating a sequence, the only reason to prefer 'seq' over an array or list, is because you want it to be lazy or have effects that happen as a result of evaluating the sequence. In that case, you want 'the whole language' to write effect code. If not, you deal with understanding code like 's' below. (List and array literals, on the other hand, are evaluated eagerly, so no need to ponder when effects happen there.)

1
2
3
4
5
6
7
8
9
10
11
12
13
let Foo() = 
    printfn "hi"
    ()
let s = seq {   // what ought this mean?
    Foo()       // eval now three unit values, or
    Foo()       // have effects when MoveNext() 
    Foo()       // but yield no results?
    }
let s2 = seq {  // clear when use language expression syntax
    do Foo()    // when MoveNext called, have effect,
    yield Foo() // yield this value (with effect)
    do Foo()    // when MoveNext again, have another effect, ...
    }
By on 1/21/2009 8:04 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper