Simple substitution encryption with Scala
encipher() deserves to be a full function with two parameters rather than just a closure within main().  It should take a key parameter first, because you can think of enciphering with a key as a transformation to be applied to the text.  (For that matter, I suggest swapping your args(0) and args(1) as well.)  The function would be more usable if it accepts and returns strings instead of lists.

Instead of writing args(0) twice and args(1) twice, you should assign text and key more appropriately, by not munging them prematurely with .toUpperCase.toList.  I also suggest using destructuring to assign both values at once. 

With the changes above, main() can look very clean.

As for the encipher() function, you can simplify it by using


for-comprehensions instead of repeated concatenation to an empty string
pattern matching instead of if-else.




object SimpSub {
  private val alphabet = ('A' to 'Z').toList

  def encipher(key: String, text: String): String = {
    // Build the cipher alphabet by concatenating the key to
    // the alphabet and removing duplicate letters
    val cipherAlphabet = (key.toUpperCase.toList ::: alphabet).distinct

    val cipherText = for (c &lt;- text.toUpperCase) yield c match {
      case ' ' =&gt; ' '
      case _   =&gt; cipherAlphabet(alphabet.indexOf(c))
    }
    return cipherText.mkString
  }


  def main(args: Array[String]) {
    val Array(text, key) = args

    println("Plaintext: " + text)
    println("Key: " + key)
    println("Ciphertext: " + encipher(key, text))
  }
}