• No categories


Whiley supports resizeable lists as a built-in data type. Thus, whenever you add an element to the list, the system automatically ensures there is enough space for it. The following illustrates the main list operations:

int sum([int] ls):
    if |ls| == 0:
        return 0
        return ls[0] + sum(ls[1..])

void ::main(System.Console console):
    ls = [1,2,3,4]
    ls = ls + [5]

In this example, we see several list operators being used: “|ls|” returns the length of list ls; “[1,2,3,4]” constructs a list containing values 1, 2, 3 and 4; “ls[0]” returns the element of list ls at index 0; “ls + [5]” returns the concatenation of ls and the list [5]; finally, “ls[1..]” returns the sublist of elements between indices 1 and |ls|. Thus, in the above example, the result printed is 15.