Scala for the Impatient

Control Structures and Functions

Copyright © Cay S. Horstmann 2015

Understand conditional and block expressions

Conditional Expressions

Block Expressions

Use the “for each” and “for comprehension” control structures

For Loops

Define functions


Understand named, default, and variable function arguments

Named and Default Arguments


Practice writing functions and using control structures


Scary looking lab


  1. Write a function that tests whether a character is a vowel (for now, a lowercase aeiou).
    def isVowel(ch: Char) = ...
  2. Did you use an if statement? If so, write it without an if. (Hint: contains)
  3. Write a function that, given a string, returns a string of all of its vowels. Use isVowel. Use a for loop.
    def vowels(s: String) = ...
  4. Repeat with a for ... yield loop. (Hint: Guards)
  5. Repeat with a recursive solution.
  6. Repeat with a while loop.
  7. Pick your favorite version. Add a parameter vowelChars with default "aeiuo" and a parameter ignoreCase with default true.
  8. Call it to find all vowels in the German word "Übeltätergehör". (Yes, those things with dots are vowels in German.)