Algebraic data types: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: Fix comment: Perl 6 --> Raku) |
(Added C#) |
||
Line 252: | Line 252: | ||
print(t); |
print(t); |
||
}</lang> |
}</lang> |
||
=={{header|C sharp}}== |
|||
Translation of several |
|||
{{works with|C sharp|8}} |
|||
<lang csharp>using System; |
|||
class Tree |
|||
{ |
|||
public static void Main() { |
|||
Tree tree = Tree.E; |
|||
for (int i = 1; i <= 16; i++) { |
|||
tree = tree.Insert(i); |
|||
} |
|||
tree.Print(); |
|||
} |
|||
private const bool B = false, R = true; |
|||
public static readonly Tree E = new Tree(B, null, 0, null); |
|||
private Tree(bool c, Tree? l, int v, Tree? r) => (IsRed, Left, Value, Right) = (c, l ?? this, v, r ?? this); |
|||
public bool IsRed { get; private set; } |
|||
public int Value { get; } |
|||
public Tree Left { get; } |
|||
public Tree Right { get; } |
|||
public static implicit operator Tree((bool c, Tree l, int v, Tree r) t) => new Tree(t.c, t.l, t.v, t.r); |
|||
public void Deconstruct(out bool c, out Tree l, out int v, out Tree r) => (c, l, v, r) = (IsRed, Left, Value, Right); |
|||
public override string ToString() => this == E ? "[]" : $"[{(IsRed ? 'R' : 'B')}{Value}]"; |
|||
public Tree Insert(int x) => Ins(x).MakeBlack(); |
|||
private Tree MakeBlack() { IsRed = false; return this; } |
|||
public void Print(int indent = 0) { |
|||
Console.WriteLine(new string(' ', indent * 4) + ToString()); |
|||
if (this == E) return; |
|||
Left.Print(indent + 1); |
|||
Right.Print(indent + 1); |
|||
} |
|||
private Tree Ins(int x) => Math.Sign(x.CompareTo(Value)) switch { |
|||
_ when this == E => (R, E, x, E), |
|||
-1 => new Tree(IsRed, Left.Ins(x) , Value, Right).Balance(), |
|||
1 => new Tree(IsRed, Left , Value, Right.Ins(x)).Balance(), |
|||
_ => this |
|||
}; |
|||
private Tree Balance() => this switch { |
|||
(B, (R, (R, var a, var x, var b), var y, var c), var z, var d) => (R, (B, a, x, b), y, (B, c, z, d)), |
|||
(B, (R, var a, var x, (R, var b, var y, var c)), var z, var d) => (R, (B, a, x, b), y, (B, c, z, d)), |
|||
(B, var a, var x, (R, (R, var b, var y, var c), var z, var d)) => (R, (B, a, x, b), y, (B, c, z, d)), |
|||
(B, var a, var x, (R, var b, var y, (R, var c, var z, var d))) => (R, (B, a, x, b), y, (B, c, z, d)), |
|||
_ => this |
|||
}; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[B8] |
|||
[B4] |
|||
[B2] |
|||
[B1] |
|||
[] |
|||
[] |
|||
[B3] |
|||
[] |
|||
[] |
|||
[B6] |
|||
[B5] |
|||
[] |
|||
[] |
|||
[B7] |
|||
[] |
|||
[] |
|||
[B12] |
|||
[B10] |
|||
[B9] |
|||
[] |
|||
[] |
|||
[B11] |
|||
[] |
|||
[] |
|||
[B14] |
|||
[B13] |
|||
[] |
|||
[] |
|||
[B15] |
|||
[] |
|||
[R16] |
|||
[] |
|||
[]</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |