Basic Construction

Category:
Sequences
Description: This sample demonstrates basic sequence construction.
Code:
let SequenceExample1() =
// Create an empty sequence
let emptySeq = Seq.empty

// Create a simple sequence
let simpleSeq = seq{1..50}
// sequence terms are not evaluated until needed

// Create a sequence using a sequence expression
let squaresSeq = seq { for i in 1..5 do yield i*i }

// Alternate form of the previous sequence expression
let squaresSeq2 = seq { for i in 1..5 -> i*i }
// '->' is equivalent to 'do yield'

// Another sequence expression example
let getDirs dir = seq { for folder in System.IO.Directory.GetDirectories(dir) -> folder }

printfn "emptySeq = %A" emptySeq
printfn "\nsimpleSeq = %A" simpleSeq
printfn "\nsquaresSeq = %A" squaresSeq
printfn "\nsquaresSeq2 = %A" squaresSeq2
printfn "\nget directories in C: %A" (getDirs @"C:\")

Execution Result:
emptySeq = seq []

simpleSeq = seq [1; 2; 3; 4; ...]

squaresSeq = seq [1; 4; 9; 16; ...]

squaresSeq2 = seq [1; 4; 9; 16; ...]

get directories in C: seq
["C:\$ASITEMP$"; "C:\$Recycle.Bin"; "C:\Boot"; "C:\Documents and Settings";
...]

Last edited Sep 14, 2011 at 2:55 AM by ttliu2000, version 1

Comments

No comments yet.