Navigation
Synopsis Insert an element at a specific position in a list.
Function list[&T] insertAt(list[&T] lst, int n, &T elm) throws IndexOutOfBounds
Usage import List;
Description Returns a new list with the value of elm inserted at index position n of the old list.
Examples
rascal>import List;
ok
rascal>insertAt([1,2,3], 1, 5);
list[int]: [1,5,2,3]
rascal>insertAt(["zebra", "elephant", "snake", "owl"], 2, "eagle");
list[str]: ["zebra","elephant","eagle","snake","owl"]
An exception is thrown when the index position is outside the list:
rascal>insertAt([1,2,3], 10, 5);
|rascal://List|(7109,1036,<335,0>,<379,83>): IndexOutOfBounds(10)
	at *** somewhere ***(|rascal://List|(7109,1036,<335,0>,<379,83>))
	at insertAt(|stdin:///|(22,1,<1,22>,<1,23>))



Questions
Question [1]. After inserting an element in a list with N elements, the new list has




Question [2].
The type of insertAt(["Jabba the Hut", "Elmo", "Slimey the Worm", "Coriander"], 2, "Little Bird") is

Question [3].
insertAt(["Nutmeg", "Joh Yowza", "Nutmeg", "Garlic", "Peppermint", "D"], 0, "Date") == 



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.