The Artima Developer Community


Errata for Programming in Scala, Third Edition
Chapter 7: Built-in Control Structures
Return to errata index.

Page 119 (PDF page 157):
gcdLoop (Listing 7.2) calculates gcd correctly for positiv numbers, while
give unreliable results for others.

scala> def gcdLoop(x: Long, y: Long): Long = {
     | var a = x
     | var b = y
     | while (a != 0) {
     | val temp = a
     | a = b % a
     | b = temp
     | }
     | b
     | }
gcdLoop: (x: Long, y: Long)Long

scala> gcdLoop(3,-7)
res1: Long = -1

scala> gcdLoop(-3,-6)
res2: Long = -3

scala> gcdLoop(0,0)
res3: Long = 0

Note: gcd must always be positive and has no meaning for (0,0).

See also: http://www.artima.com/forums/flat.jsp?forum=282&thread=388491
Page 120 (PDF page 158):
scala> def greet() = { println("hi") }
greet: ()Unit

...

Because no equals sign precedes its body, greet is defined to be a proce-
dure with a result type of Unit .

?

There is an equal sign which precedes its body, I suppose the reason it's
()Unit is because println returns ()Unit.

FIXED in 3ed 4th printing
Page 120 (PDF page 158):
http://i.imgur.com/MBwApXw.png

FIXED in 3ed 4th printing
Page 120 (PDF page 158):
It reads,

"Because no equals sign precedes its body, greet is defined to be a
procedure with a result type of Unit."

while in the example given just above, greet is defined with `=' thus:
def greet() = { println("hi") }

this was not there in the 2nd edition.

FIXED in 3ed 4th printing
Page 120 (PDF page 158):
7.2 WHILE LOOPS
In the example annotated with "Because no equals sign precedes its body,
greet is defined to be a procedure with a result type of Unit.", the
equal sign precedes the method body - 'def greet() = { println("hi") }'

FIXED in 3ed 45th printing
Page 120 (PDF page 158):
the greet function is:
  def greet() = { println("hi") }

the text says "Because no equals sign precedes its body, greet is defined
to be a procedure with a result type of Unit."  this statement doesn't
appear to describe the definition of greet correctly as there is clearly
an equals sign preceding the body of greet.  since the code is using
function syntax, and not procedure syntax, greet's return type is
inferred from the last expression in the body, which happens to be Unit. 


further, isn't the "procedure" syntax described in the text discouraged
at this point?

FIXED in 3ed 4th printing
Page 120 (PDF page 158):
In middle of page: "Because no equals sign precedes its body, greet is
defined to be a procedure with a result type of Unit"

Actually the code snippet includes the equals sign. Maybe since procedure
syntax is deprecated, snippet was updated, but text wasn't changed to
reflect this?

FIXED in 4th printing
Page 127 (PDF page 165):
It is written,

"For each of these it generates an Iterator[String], the result of the
fileLines method" while the fileLines method actually results in a
List[String] due to the last ".toList" function as defined in Listing
7.8:

"def fileLines(file: java.io.File) =
scala.io.Source.fromFile(file).getLines().toList"

FIXED in 3ed 4th printing. Changed toList to toArray and updated text.
Page 127 (PDF page 165):
For each of these it gen-
erates an Iterator[String] , the result of the fileLines method, whose
definition is shown in Listing 7.8. An Iterator offers methods next and
hasNext that allow you to iterate over a collection of elements. This
ini-
tial iterator is transformed into another Iterator[String] containing
only
trimmed lines that include the substring "for" .

^^^

In the Listing 7.8 the result of fileLines is List[String], not
Iterator[String] (scala.io.Source.fromFile(file).getLines().toList). 

Besides, even if it was Iterator[String] `for` comprehension expands to
flatMap/map, not to next & hasNext. So mentioning next and hasNext in
this context is probably misleading?

FIXED in 3ed 4th printing
Page 129 (PDF page 167):
Foot note says
Although you must always surround the case statements of a catch clause
in paren- theses, try and finally do not require parentheses if they
contain only one expression. For example, you could write: try t() catch
{ case e: Exception => ... } finally f().

shouldn't parentheses be replaced by curly braces

FIXED in 3ed 4th printing

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.