![]() |
|
Navigation |
Synopsis Get the first element(s) from a list.
Function
Usage
import List;
Description
Examples
rascal>import List;
ok
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>head([]);
|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>))
Questions
Question [1].
![]() ![]()
Question [2].
![]() ![]() ![]() |