Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*A: Masking it as a sequence*)
- type Node<'a> = Empty | Node of 'a * Node<'a>
- let cyclic (n:Node<_>) : _ =
- let rn = ref n
- let rec next _ =
- match !rn with
- | Empty -> rn := n; next Unchecked.defaultof<_>
- | Node(v, x) -> rn := x; v
- Seq.initInfinite next
- (*Usage:*)
- let nodes = Node(1, Node(2, Node(3, Empty)))
- cyclic <| nodes |> Seq.take 40 (*val it : seq<int> = seq [1; 2; 3; 1; ...]*)
- (*B: Using lazy*)
- #nowarn "40"
- type LNode<'a> = Empty | LNode of 'a * Lazy<LNode<'a>>
- let rec x = LNode(1, lazy LNode(2, lazy x))
- (*Usage:*)
- let first =
- match x with
- | LNode(1, Lazy(LNode(2,first))) -> first.Value
- | _ -> Empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement