// Exercise 1: pattern matching with records (easy) // // Implement with switch EXPRESSIONS (no if/else chains): // Describe(Shape s): // Circle with Radius 0 → "point"; Circle → "circle r="; // Rect with Width == Height → "square "; Rect → "rect x"; null → "nothing". // Grade(int score): >= 90 "A"; 80..89 "B"; < 0 or > 100 "invalid"; otherwise "C". (Check invalid first.) // Format numbers with the invariant culture (FormattableString.Invariant) so "2.5" doesn't become "2,5". // Summarize(int[] xs) with list patterns: [] "empty"; [x] "one: x"; [a, b] "two: a,b"; [first, .., last] "first..last". // // Run with: dotnet run (inside a console project). using System; abstract record Shape; record Circle(double Radius) : Shape; record Rect(double Width, double Height) : Shape; class Program { static string Describe(Shape s) => ""; // TODO static string Grade(int score) => ""; // TODO static string Summarize(int[] xs) => ""; // TODO static void Main() { int pass = 0, total = 0; void Check(string name, bool ok) { total++; if (ok) pass++; Console.WriteLine($"{(ok ? "PASS" : "FAIL")}: {name}"); } Check("point", Describe(new Circle(0)) == "point"); Check("circle", Describe(new Circle(2.5)) == "circle r=2.5"); Check("square", Describe(new Rect(3, 3)) == "square 3"); Check("rect", Describe(new Rect(2, 5)) == "rect 2x5"); Check("null", Describe(null) == "nothing"); Check("grades", Grade(95) == "A" && Grade(80) == "B" && Grade(79) == "C" && Grade(101) == "invalid" && Grade(-1) == "invalid"); Check("lists", Summarize(new int[0]) == "empty" && Summarize(new[] { 7 }) == "one: 7" && Summarize(new[] { 1, 2 }) == "two: 1,2" && Summarize(new[] { 1, 2, 3, 9 }) == "1..9"); var c = new Circle(1); Check("records: value equality and with", c == new Circle(1) && (c with { Radius = 2 }) == new Circle(2) && c.Radius == 1); Console.WriteLine($"--- {pass}/{total} checks passed ---"); } }