Scala for the Impatient: Chapter 2 exercises solutions

I’ve started going through the book: Scala for the Impatient by Cay Horstmann. I’m posting the solution to chapter 2 exercises below.

Advertisements

11 thoughts on “Scala for the Impatient: Chapter 2 exercises solutions

  1. Wouldn’t it be even better to use BigInt type? Replacing “Hello” by a longer sentence, the function above fails to give a correct answer, doesn’t it?

  2. Nice work. I think the answer to exercise 5 is wrong. A procedure doesn’t return anything and hence it shall be without an ‘=’:
    def countDown( num: Int ) { for(i <- num to (0, -1)) println(i) }

      • The type of the function is Unit implicitly. However, I agree that it’s better to explicitly mention the Unit type. I’ve updated the code accordingly.

        Personally, I prefer not to omit the ‘=’ for procedures. I find it easier to stick to a consistent style of always using ‘=’ when defining functions and methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s