Basic List Construction 2 - vilinski/nemerle GitHub Wiki

Basic List Construction 2

  • Category: Lists, Tuples and Options
  • Description: This sample demonstrates a simple generative list comprehension to specify lists
  • Code:
using System;
using System.Console;
using Nemerle;

def data1 = $[(x, x * x), x in [0 .. 20]];

// This uses a nested loop
def data2 = $[(x, y, x * y), x in [0 .. 5], y in [0 .. 5]];

// This uses a filter
def data3 = $[(x, y, x * y), x in [0 .. 5], y in [0 .. 5], x > y];

WriteLine($"data1 = \n$data1\n\n");
WriteLine($"data2 = \n$data2\n\n");
WriteLine($"data3 = \n$data3\n\n");
  • Execution Result:
data1 = 
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64), (9, 81), (10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225), (16, 256), (17, 289), (18, 324), (19, 361), (20, 400)]


data2 = 
[(0, 0, 0), (0, 1, 0), (0, 2, 0), (0, 3, 0), (0, 4, 0), (0, 5, 0), (1, 0, 0), (1, 1, 1), (1, 2, 2), (1, 3, 3), (1, 4, 4), (1, 5, 5), (2, 0, 0), (2, 1, 2), (2, 2, 4), (2, 3, 6), (2, 4, 8), (2, 5, 10), (3, 0, 0), (3, 1, 3), (3, 2, 6), (3, 3, 9), (3, 4, 12), (3, 5, 15), (4, 0, 0), (4, 1, 4), (4, 2, 8), (4, 3, 12), (4, 4, 16), (4, 5, 20), (5, 0, 0), (5, 1, 5), (5, 2, 10), (5, 3, 15), (5, 4, 20), (5, 5, 25)]


data3 = 
[(1, 0, 0), (2, 0, 0), (2, 1, 2), (3, 0, 0), (3, 1, 3), (3, 2, 6), (4, 0, 0), (4, 1, 4), (4, 2, 8), (4, 3, 12), (5, 0, 0), (5, 1, 5), (5, 2, 10), (5, 3, 15), (5, 4, 20)]

[Copyright ©](Terms of use, legal notice)