public static Node Fail = new Node { Val = null, Next = null };
public static Node Nil = new Node { Val = null, Next = null };
public string Val { get; private set; }
public Node Next { get; private set; }
public Node(string val, Node next) { Val = val; Next = next ?? Nil; }
public static Func<Node, Node> InitQuery() { return (node) => node; }
public static class NodeExtensions
public static Node ToDomainNodes(this string target)
.Aggregate(Node.Nil, (node, dom) => new Node(dom, node));
public static Func<Node, Node> ThenMatch(this Func<Node, Node> target, string val)
var firstResult = target(node);
if (firstResult != Node.Fail)
return (firstResult == Node.Fail || firstResult == Node.Nil || firstResult.Val != val)
public static bool IsSubdomainOf(this string fqdn, string domain)
.Aggregate(Node.InitQuery(), (query, domainPart) => query.ThenMatch(domainPart))
(fqdn.ToDomainNodes()) != Node.Fail;
public static void Main()
Console.WriteLine("Hello World");
Console.WriteLine("foo.bar.baz.example.com".IsSubdomainOf("baz.example.com"));