scala empty list
Scala Empty List
In Scala, an empty list can be represented using the Nil
keyword. Nil
is an object of type List[Nothing]
and represents an empty list. It is commonly used as the base case when defining recursive functions on lists.
Here is an example of how to create an empty list in Scala:
val emptyList: List[Int] = Nil
In the above example, emptyList
is a variable of type List[Int]
that is assigned the value Nil
, which represents an empty list of integers.
Please note that Nil
is immutable, meaning that you cannot modify it directly. If you need to add elements to a list, you can use the ::
operator to prepend elements to an existing list or use other list operations to create a new list.
I hope this helps!