Navigation
Synopsis List values.
Syntax [ Exp1, Exp2, ... ]
Types
Exp1 Exp2 ... [ Exp1, Exp2, ... ]
T1 T2 ... list[lub(T1, T2, ... )]
Usage import List; (included in Prelude)
Description A list is an ordered sequence of values and has the following properties:
  • All elements have the same static type.
  • The order of the elements matters.
  • A list may contain an element more than once.
The type of a list has the form list[T], where T is an arbitrary type.

When a value or variable of type list occurs inside a list, that list value is inserted as list element. To achieve splicing of these elements, i.e., the insertion of the elements of the list value rather than the whole list, it has to be prefixed by the splice operator *.

The following operators are provided on list:
  • Append: Append an element at the end of a list
  • Comprehension: A list comprehension generates a list value.
  • Concatenation: Concatenate two lists.
  • Difference: The difference between two lists.
  • Equal: Equality on lists.
  • in: Membership test on list elements.
  • Insert: add an element in front of a list
  • Intersection: Intersection of two lists.
  • NotEqual: Not equal operator on lists.
  • notin: Negated membership test on lists.
  • Product: Compute the product of two lists.
  • Slice: Retrieve a slice of a list.
  • Splice: Splice the elements of a list in an enclosing list.
  • StrictSubList: The strict sublist operator on lists.
  • StrictSuperList: The strict super list operator on lists.
  • SubList: The sublist operator on lists.
  • Subscription: Retrieve a list element via its index.
  • SuperList: The super list operator on lists.
The following functions are provided for lists:
  • delete: Delete an element from a list.
  • distribution: Get the distribution of the elements of the list. That is how often does each element occur in the list?
  • drop: Drop elements from the head of a list.
  • dup: Remove multiple occurrences of elements in a list. The first occurrence remains.
  • getOneFrom: Pick a random element from a list.
  • head: Get the first element(s) from a list.
  • headTail: Split a list in a head and a tail.
  • index: A list of legal index values of a list.
  • indexOf: Index of first occurrence of an element in a list.
  • insertAt: Insert an element at a specific position in a list.
  • intercalate: Join a list of values into a string separated by a separator.
  • isEmpty: Test whether a list is empty.
  • itoString: Convert a list to an indented string.
  • last: Return the last element of a list, if any.
  • lastIndexOf: Return index of last occurrence of elt in lst, or -1 if elt is not found.
  • mapper: Apply a function to all list elements and return list of results.
  • max: Determine the largest element in a list.
  • merge: Merge the elements of two sorted lists into one list.
  • min: Determine the smallest element in a list.
  • mix: Mix the elements of two lists.
  • permutations: Compute all permutations of a list.
  • pop: Pop top element from list, return a tuple.
  • prefix: Return all but the last element of a list.
  • push: Push an element in front of a list.
  • reducer: Apply a function to successive elements of list and combine the results (deprecated).
  • reverse: Reverse a list.
  • size: Determine the number of elements in a list.
  • slice: Compute a sublist of a list.
  • sort: Sort the elements of a list.
  • split: Split a list into two halves.
  • sum: Sum the elements of a list.
  • tail: Get the tail element(s) from a list.
  • take: Get number of elements from the head of a list.
  • takeOneFrom: Remove an arbitrary element from a list, returns the element and the modified list.
  • takeWhile: Take elements from the front of the list as long as a predicate is true.
  • toMap: Convert a list of pairs to a map; first elements are associated with a set of second elements.
  • toMapUnique: Convert a list of tuples to a map; result must be a map.
  • top: Take the top element of a list.
  • toRel: Convert a list to a relation.
  • toSet: Convert a list to a set.
  • toString: Convert a list to a string.
  • unzip: Make a pair (triple) of lists from a list of pairs (triples).
  • upTill: Returns the list 0,1..n-1.
  • zip: Make a list of pairs from two (three) lists of the same length.
Examples
rascal>[1, 2, 3];
list[int]: [1,2,3]
rascal>[<1,10>, <2,20>, <3,30>];
lrel[int,int]: [
  <1,10>,
  <2,20>,
  <3,30>
]
rascal>[1, "b", 3];
list[value]: [1,"b",3]
rascal>[<"a",10>, <"b",20>, <"c",30>];
lrel[str,int]: [
  <"a",10>,
  <"b",20>,
  <"c",30>
]
rascal>[["a", "b"], ["c", "d", "e"]];
list[list[str]]: [
  ["a","b"],
  ["c","d","e"]
]
List splicing works as follows: by prefixing L by the splice operator, its elements are included as elements in the enclosing list:
rascal>L = [1, 2, 3];
list[int]: [1,2,3]
rascal>[10, L, 20];
list[value]: [
  10,
  [1,2,3],
  20
]
rascal>[10, *L, 20];
list[int]: [10,1,2,3,20]

Questions
Question [1]. The type of a list is determined by:




Question [2]. Fill in the missing operator.
["Lychee", "Countess Von Backwards", "C", "Pumpkin"]  "Bruno" == ["Lychee","Countess Von Backwards","C","Pumpkin","Bruno"]

Question [3]. Fill in the missing operator.
[47,15,95,62,61,91,54,53,77,73]  [84,20,47,15,75,43,61,91,53,41] == [95,62,54,77,73]

Question [4]. Fill in the missing operator.
([24, 47, 52, 60]  [60,52,47,24]) == false

Question [5]. Fill in the missing operator.
[92,23,82,45,37]  [62,92,25,23,82,45,2] == [92,23,82,45]

Question [6]. Fill in the missing operator.
53  [60,55,53,75,35] == true

Question [7]. Fill in the missing operator.
[74,36]  [74,36,98] == true

Question [8]. Fill in the missing operator.
[2, 19, 17]  [-14, -13] == [2,19,17,-14,-13]

Question [9]. Fill in the missing operator.
6  [7, 3, 7, 7] == true



Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.