List methodsPair classjavadocscaladoc and vscaladoc
(prettier)List Methodslength head tail isEmpty:: ::: + - -- == != < <= > >=(n) take drop dropRight
slice indexOf lastIndexOfcount exists dropWhile filter find
findIndexOf forall partition remove span takeWhilemap reverseMap flatMap foreach
sortreduceLeft reduceRight
foldLeftfoldRight /: :\intersection union zip zipAll zipWithIndex
mkStringA-Z0-9()[]{}`'".,;: are right associative; all others are
left associative
a :: b :: Nil is a :: (b :: Nil)
().List(1, 2) ::: List(3, 4) is List(1, 2, 3, 4) // same as ++ List(1, 2) < List(3) is true // lexicographic order List(1, 2, 3, 4) -- List(1, 3) is List(2, 4)
() are used for indexed access—not []
List(17, 29)(1) is 29
slice takes sublist (with index values like in Java's
String.substring)
List(2, 3, 5, 7).slice(1, 3) is List(3, 5)
drop takefilterdef randList(len : Int, n : Int) = (1 to len).map((x : Int) => gen.nextInt(n))
BooleanList(2,3,5,7).partition(isEven) is (List(2),List(3, 5, 7))
S, T, U are any types, then we
have tuple types (S, T), (S, T, U)("Hello", 1729) is an instance of (String,
Int)_1 _2 etc. to access members
(not zero-based!)
("Hello", 1729)._2 is 1729
(day,
month, year). Make (or use) a Date class.List(a, b, c) is
a + b + c = 0 + a + b + c = ((0 + a) + b) + c
foldLeft or the /: operator
def sum(lst: List[Int]) = (0 /: lst) ((x, y) => x + y)
/: indicates the tree shape
+
/ \
+ c
/ \
+ b
/ \
0 a
def fac(n : Int) = (1 /: (1 to n)) (_ * _)
foldRight operator works right-to-left: a + (b +
(c + 0))mkStringtoString produces the familiar List(1, 2,
3)1 | 2 | 3?
lst.mkString("|")
One fewer separator than elements
[1 | 2 | 3]?
lst.mkString("[", "|", "]")

- operator do? Look it up in Scaladoc. Run a
couple of experiments. What happens if a value occurs more than once in the
list? Tell me which experiments you ran, and what you concluded.== only checks for object identity. For example,
String a = "Hel"; System.out.println(a + "lo" ==
"Hello") prints false. (You need to use
equals to test for structural equality.) What is the situation
for == with Scala lists? Tell me which experiments you ran,
and what you concluded.take and drop do? Give a brief
explanation and an example for each.take and
dropRight?span in Scaladoc. Make an example
that demonstrates how span works. What is your example, and
what value does it produce?span method returns a pair. Show how you can get at each
of the elements in that pair./: folding operator to concatenate all strings in a
List[String], separating them with spaces. For example, if you
start with val lst = List("Hello", "Scala", "World"), you
should produce an expression involving lst and /:
that yields "Hello Scala World". (Hint: It is very easy to get
" Hello Scala World". The challenge is to get rid of the first
space. Consider the case of computing the maximum. Find a suitable function whose first argument is the maximum of all elements visited so far, and whose second argument is the next element.
What the code for your function maximum(lst : List[Int]) :
Int? (You may assume that the list has length > 0)
def mystery(lst : List[Int]) = (List[Int]() /: lst) ((x, y) => if (y % 2 == 0) x else x + y)
Explain how the function works.