序列(Sequences),像列表一样有序集合,但是需要时可以计算序列表达式中的元素,它们不是立即计算的,因此,它们被用来表示无限的数据结构。
序列使用以下语法定义-
seq { expr }
如,
let seq1=seq { 1 .. 10 }
与列表类似,您可以使用范围和理解来创建序列。
序列表达式是您可以编写的用于创建序列的表达式,这些可以做到-
以下示例演示了概念-
(* Sequences *) let seq1 = seq { 1 .. 10 } (* ascending order and increment*) printfn "The Sequence: %A" seq1 let seq2 = seq { 1 .. 5 .. 50 } (* descending order and decrement*) printfn "The Sequence: %A" seq2 let seq3 = seq {50 .. -5 .. 0} printfn "The Sequence: %A" seq3 (* using yield *) let seq4 = seq { for a in 1 .. 10 do yield a, a*a, a*a*a } printfn "The Sequence: %A" seq4
编译并执行程序时,将产生以下输出-
The Sequence: seq [1; 2; 3; 4; ...] The Sequence: seq [1; 6; 11; 16; ...] The Sequence: seq [50; 45; 40; 35; ...] The Sequence: seq [(1, 1, 1); (2, 4, 8); (3, 9, 27); (4, 16, 64); ...]
以下程序打印从1到50的质数-
(* Recursive isprime function. *) let isprime n = let rec check i = i > n/2 || (n % i <> 0 && check (i + 1)) check 2 let primeIn50 = seq { for n in 1..50 do if isprime n then yield n } for x in primeIn50 do printfn "%d" x
编译并执行程序时,将产生以下输出-
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
该程序创建一个空序列,并在以后填充它-
(* Creating sequences *) let emptySeq = Seq.empty let seq1 = Seq.singleton 20 printfn"The singleton sequence:" printfn "%A " seq1 printfn"The init sequence:" let seq2 = Seq.init 5 (fun n -> n * 3) Seq.iter (fun i -> printf "%d " i) seq2 printfn"" (* converting an array to sequence by using cast *) printfn"The array sequence 1:" let seq3 = [| 1 .. 10 |] :> seq<int> Seq.iter (fun i -> printf "%d " i) seq3 printfn"" (* converting an array to sequence by using Seq.ofArray *) printfn"The array sequence 2:" let seq4 = [| 2..2.. 20 |] |> Seq.ofArray Seq.iter (fun i -> printf "%d " i) seq4 printfn""
编译并执行程序时,将产生以下输出-
The singleton sequence: seq [20] The init sequence: 0 3 6 9 12 The array sequence 1: 1 2 3 4 5 6 7 8 9 10 The array sequence 2: 2 4 6 8 10 12 14 16 18 20
请注意-
Seq.empty - 方法创建一个空序列。
Seq.singleton - 方法仅创建一个指定元素的序列。
Seq.init - 方法创建一个序列,使用给定函数为其创建元素。
Seq.ofArray和Seq.ofList <'T>方法 - 从数组和列表创建序列。
Seq.iter - 方法允许迭代序列。
Seq.unfold方法从计算函数生成一个序列,该函数获取状态并将其转换为序列中的每个后续元素。
以下函数产生前20个自然数-
链接:https://www.learnfk.comhttps://www.learnfk.com/fsharp/fsharp-sequences.html
来源:LearnFk无涯教程网
let seq1 = Seq.unfold (fun state -> if (state > 20) then None else Some(state, state + 1)) 0 printfn "The sequence seq1 contains numbers from 0 to 20." for x in seq1 do printf "%d " x printfn" "
编译并执行程序时,将产生以下输出-
The sequence seq1 contains numbers from 0 to 20. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Seq.truncate方法从另一个序列创建一个序列,但将序列限制为指定数量的元素。
Seq.take方法创建一个新序列,该序列从序列的开头开始包含指定数量的元素。
let mySeq = seq { for i in 1 .. 10 -> 3*i } let truncatedSeq = Seq.truncate 5 mySeq let takeSeq = Seq.take 5 mySeq printfn"The original sequence" Seq.iter (fun i -> printf "%d " i) mySeq printfn"" printfn"The truncated sequence" Seq.iter (fun i -> printf "%d " i) truncatedSeq printfn"" printfn"The take sequence" Seq.iter (fun i -> printf "%d " i) takeSeq printfn""
编译并执行程序时,将产生以下输出-
The original sequence 3 6 9 12 15 18 21 24 27 30 The truncated sequence 3 6 9 12 15 The take sequence 3 6 9 12 15
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕