Synopsis Get the first element(s) from a list.
  1. &T head(list[&T] lst) throws EmptyList
  2. list[&T] head(list[&T] lst, int n) throws IndexOutOfBounds
Usage import List;
  1. Returns the first element of a list or throws EmptyList when the list is empty. This is identical to top.
  2. Returns the first n elements of a list or throws IndexOutOfBounds when the list is too short. This is similar to take.
rascal>import List;
Get the first element:
rascal>head([1, 2, 3]);
int: 1
rascal>head(["zebra", "elephant", "snake", "owl"]);
str: "zebra"
An exception is thrown when taking the head of an empty list:
|rascal://List|(3636,1037,<170,0>,<209,51>): EmptyList()
	at *** somewhere ***(|rascal://List|(3636,1037,<170,0>,<209,51>))
	at head(|stdin:///|(5,2,<1,5>,<1,7>))

Get the first n elements:
rascal>head([1, 2, 3, 4], 2);
list[int]: [1,2]
rascal>head(["zebra", "elephant", "snake", "owl"], 2);
list[str]: ["zebra","elephant"]
An exception is thrown when the second argument exceeds the length of the list:
rascal>head([1, 2, 3, 5], 5);
|rascal://List|(4713,113,<212,0>,<213,71>): IndexOutOfBounds(4)
	at *** somewhere ***(|rascal://List|(4713,113,<212,0>,<213,71>))
	at head(|stdin:///|(19,1,<1,19>,<1,20>))

Question [1].
The type of head([-1, 16, 13, 0]) is

Question [2].
head([-20, 3, 11]) == 

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