Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
tail
list
communitycreator

What is the list.tail method in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The list.tail function in Scala returns the reference of all the elements in the list except the first element.

Figure 1 shows a visual representation of the list.tail function.

Figure 1: Visual representation of the list.tail function

Syntax

list_name.tail
// where the list_name is the name of the list

Parameters

This function does not require any parameters.

Return value

The list.tail function returns the reference of all the elements in the list except the first element.

  • It does not remove that element from the list.
  • If the list is empty, then it throws an error.

Code

The following code shows how to use the list.tail function in Scala.

object Main extends App {
var list = List(1,3,4,2,0)
//list = 1->3->4->2->0
println("Following are the elements in List before using list.tail: " + list);
println("The elements on the tail of list: "+list.tail);
println("Following are the elements in list after using list.tail: " + list);
}

RELATED TAGS

scala
tail
list
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring