The Artima Developer Community


Errata for Programming in Scala
Chapter 16: Working with Lists
Return to errata index.

Page 199 (PDF page 237):
I'm reading the chinese version of Programming in Scala 2 Edition so I'm
not sure about the real page number in english version.
In chapter 16.6, Reverse List, the following method
def rev[T](xs: List[T]): List[T] = xs mathch {
  case List() => xs
  case x :: xs1 => rev(xs1) ::: List(x)
}
The book says the complexity of this method is
  n + (n-1) + ... + 1 = (1+n)*n/2
I think rev on list of length 1 needs only one 'append' step,
rev on list of length 2 needs 2 steps(one more than rev on list of length
1),
rev on list of length 3 needs 3 steps(one more than rev on list of length
2),
...
...
so rev on list of length n needs n steps(one more than rev on list of
length n-1) and there is no duplicate invocation on each length of
sub-list.
I have also modified the previous method to prove that the complexity is
n :
var countOfAppend = 0
def rev[T](xs: List[T]): List[T] = xs mathch {
  case List() => xs
  case x :: xs1 =>
    countOfAppend += 1
    rev(xs1) ::: List(x)
}
If I test the following code:
val list = 1 :: 2 :: 3 :: 4 :: 5:: Nil
rev(list)
then I will get countOfAppend of 5(not 15), am I wrong?
Page 300 (PDF page 330):
in the last paragraph: "a, b andc" -> "a, b and c"

FIXED
Page 322 (PDF page 352):
First line: "1, 2, 3@" -> "1, 2, 3"

FIXED

Page number: Book type: Paperback book PDF eBook
Book version: (Or build date. Found on back of title page.)
Your feedback:
Your name: (optional)
Your email address: (optional) (will not be published)

Copyright © 2024 Artima, Inc. All rights reserved.