
Professional Scala
By :

Lists are probably the most commonly used data structures in Scala programs. Learning how to work with lists is important both from a data structure standpoint but also as an entry point to designing programs around recursive data structures.
In order
to be able to use l
ists
, one must learn how to construct them.
Lists
are recursive in nature, and build upon two basic building blocks:
Nil
(representing the empty list) and
::
(pronounced cons, from the
cons
function of most Lisp dialects).
We will now create Lists in Scala:
Start the Scala
REPL
, which should provide you with a prompt:
$ scala
Create a
list
of strings using the following:
scala> val listOfStrings = "str1" :: ("str2" :: ("str3" :: Nil)) listOfStrings: List[String] = List(str1, str2, str3)
Show
that the
::
operation is the right associative by omitting the parentheses and getting the same result:
scala> val listOfStrings = "str1" :: "str2" :: "str3" :: Nil listOfStrings: List...
Change the font size
Change margin width
Change background colour