如果我有一个由递归函数生成的序列,如下所示:
let rec genConst t = seq { yield t ; yield! genConst (t) }
let inf = genConst 1
如何测试 inf 的长度是否为无限?
如果我有一个由递归函数生成的序列,如下所示:
let rec genConst t = seq { yield t ; yield! genConst (t) }
let inf = genConst 1
如何测试 inf 的长度是否为无限?
如果它有助于您的特定情况,您可以定义一个标记序列无限的包装器类型:
type InfiniteSeq<'T> = private Infinite of seq<'T> with
interface seq<'T> with
member x.GetEnumerator() = let (Infinite s) = x in s.GetEnumerator()
interface System.Collections.IEnumerable with
member x.GetEnumerator() = let (Infinite s) = x in s.GetEnumerator() :> _
module Seq =
let infinite source = Infinite source
let rec genConst t = seq { yield t ; yield! genConst (t) } |> Seq.infinite
let firstFive = genConst 1 |> Seq.take 5
你得到多态性,seq<_>
类型表示附加行为。