2 people like it.

Tree using sequences

Tree implementation using sequences.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
type 'a Tree = Node of 'a * seq<'a Tree> * seq<'a Tree>

let addL a t = 
    match t with
    | Node (v,l,r) -> Node (v,seq {yield! l; yield a},r)

let addR a t = 
    match t with
    | Node (v,l,r) -> Node (v,l,seq {yield! r; yield a})

let removeL a t = 
    match t with
    | Node (v,l,r) -> Node (v,Seq.filter (fun i -> i = a) l ,r)

let removeR a t = 
    match t with
    | Node (v,l,r) -> Node (v,l , Seq.filter (fun i -> i = a) r)

let newTree v = Node (v,Seq.empty,Seq.empty)

let tree = newTree 1 |> addL (newTree 2) |> addR (newTree 3)
union case Tree.Node: 'a * seq<'a Tree> * seq<'a Tree> -> 'a Tree
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
type 'a Tree = | Node of 'a * seq<'a Tree> * seq<'a Tree>

Full name: Script.Tree<_>
val addL : a:'a Tree -> t:'a Tree -> 'a Tree

Full name: Script.addL
val a : 'a Tree
val t : 'a Tree
val v : 'a
val l : seq<'a Tree>
val r : seq<'a Tree>
val addR : a:'a Tree -> t:'a Tree -> 'a Tree

Full name: Script.addR
val removeL : a:'a Tree -> t:'a Tree -> 'a Tree (requires equality)

Full name: Script.removeL
val a : 'a Tree (requires equality)
val t : 'a Tree (requires equality)
val v : 'a (requires equality)
val l : seq<'a Tree> (requires equality)
val r : seq<'a Tree> (requires equality)
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val i : 'a Tree (requires equality)
val removeR : a:'a Tree -> t:'a Tree -> 'a Tree (requires equality)

Full name: Script.removeR
val newTree : v:'a -> 'a Tree

Full name: Script.newTree
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
val tree : int Tree

Full name: Script.tree
Raw view New version

More information

Link:http://fssnip.net/4x
Posted:6 years ago
Author:Ankur Dhama
Tags: sequence , tree