-
Notifications
You must be signed in to change notification settings - Fork 89
Lists and Pattern Matching
NN--- edited this page Apr 18, 2012
·
3 revisions
- Category: Lists, Tuples and Options
- Description: This sample shows simple uses of lists and functions that pattern match on them.
- Code:
using Nemerle;
using System;
using System.Console;
def data = [1, 2, 3, 4];
WriteLine($"data = $data");
WriteLine($"data.Head = $(data.Head)");
WriteLine($"data.Tail = $(data.Tail)");
WriteLine($"data.Length = $(data.Length)");
def consume(data)
{
| 1 :: rest => WriteLine("matched a 1"); rest
| 2 :: 3 :: rest => WriteLine("matched a 2 and 3"); rest
| [4] => WriteLine("matched a 4"); []
| _ => WriteLine("unexpected!"); []
}
def data = consume(data);
def data = consume(data);
def data = consume(data);
WriteLine($"At end of list? $(data is [])")
- Execution Result:
data = [1, 2, 3, 4]
data.Head = 1
data.Tail = [2, 3, 4]
data.Length = 4
matched a 1
matched a 2 and 3
matched a 4
At end of list? True
[Copyright ©](Terms of use, legal notice)