Created
October 26, 2011 23:26
-
-
Save hodzanassredin/1318323 to your computer and use it in GitHub Desktop.
immutable structures in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace Tree | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var cell = new Cell<int>(1); | |
| Console.WriteLine(cell); | |
| var lst = new List<int>(1, new List<int>(2, new List<int>(3, List<int>.Nil))); | |
| Console.WriteLine(List<int>.Nil); | |
| Console.WriteLine(lst); | |
| var subTree0 = new Tree<string>("subTree0-1", Tree<string>.Nil); | |
| var subTree1 = new Tree<string>("subTree1-1", new Tree<string>("subTree1-2", Tree<string>.Nil)); | |
| var subTree2 = new Tree<string>("subTree2-1", new Tree<string>("subTree2-2", new Tree<string>("subTree2-3", Tree<string>.Nil))); | |
| var subrees = new List<Tree<string>>(subTree1, new List<Tree<string>>(subTree2, List<Tree<string>>.Nil)); | |
| var tree = new Tree<string>("tree", subrees); | |
| Console.WriteLine(subTree0); | |
| Console.WriteLine(subTree1); | |
| Console.WriteLine(subTree2); | |
| Console.WriteLine(tree); | |
| Console.ReadKey(); | |
| } | |
| } | |
| public class Cell<T> | |
| { | |
| public Cell(T value) | |
| { | |
| Value = value; | |
| } | |
| public T Value { get; private set; } | |
| public override string ToString() | |
| { | |
| if (Value != null) return Value.ToString(); | |
| return "Null"; | |
| } | |
| } | |
| public class List<T> | |
| { | |
| public static readonly List<T> Nil = new List<T>(); | |
| private T _head; | |
| protected bool IsNil = false; | |
| public T Head | |
| { | |
| get | |
| { | |
| if (IsNil) throw new ArgumentException(); | |
| return _head; | |
| } | |
| private set { _head = value; } | |
| } | |
| private List<T> _tail; | |
| public List<T> Tail | |
| { | |
| get | |
| { | |
| if (IsNil) throw new ArgumentException(); | |
| return _tail; | |
| } | |
| private set { _tail = value; } | |
| } | |
| protected List() | |
| { | |
| IsNil = true; | |
| } | |
| public List(T head, List<T> tail) | |
| { | |
| if (tail == null) throw new ArgumentException(); | |
| _head = head; | |
| _tail = tail; | |
| } | |
| public override string ToString() | |
| { | |
| return "[ " + ToStr() + " ]"; | |
| } | |
| public string ToStr() | |
| { | |
| if (IsNil) return "Nil"; | |
| else return (_head == null ? "Null" : _head.ToString()) + ":" + Tail.ToStr(); | |
| } | |
| } | |
| public class Tree<T> : List<Tree<T>> | |
| { | |
| public new static readonly Tree<T> Nil = new Tree<T>(); | |
| public T Value { get; private set; } | |
| private Tree() { } | |
| public Tree(T value, Tree<T> nodes) | |
| : base(nodes, List<Tree<T>>.Nil) | |
| { | |
| Value = value; | |
| } | |
| public Tree(T value, List<Tree<T>> subtrees) | |
| : base(subtrees.Head, subtrees.Tail) | |
| { | |
| Value = value; | |
| } | |
| public override string ToString() | |
| { | |
| if (IsNil) return "TreeNil"; | |
| var val = (Value != null)? Value.ToString() : "Null"; | |
| return "(" + val + " : " + base.ToString() + ")"; | |
| } | |
| } | |
| //this doesnt work with inheritance | |
| //public class List<T> | |
| //{ | |
| // public static readonly List<T> Nil = new List<T>(default(T), null); | |
| // public T Head { get; private set; } | |
| // public List<T> Tail { get; private set; } | |
| // protected List() | |
| // { | |
| // } | |
| // private List(T head, List<T> tail) | |
| // { | |
| // Head = head; | |
| // Tail = tail; | |
| // } | |
| // public List<T> Cons(T head) | |
| // { | |
| // return new List<T>(head, this); | |
| // } | |
| // public override string ToString() | |
| // { | |
| // if (Tail == null) return "Nil"; | |
| // else return Head + ":" + Tail; | |
| // } | |
| //} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment