Skip to content

Instantly share code, notes, and snippets.

@infomaven
Last active November 8, 2015 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infomaven/735b72d3db70ad603ed2 to your computer and use it in GitHub Desktop.
Save infomaven/735b72d3db70ad603ed2 to your computer and use it in GitHub Desktop.
Classic program that prints strings based on results of using MOD function against a List
/*
Write a program that prints out the numbers 1 to 100 (inclusive).
If the number is divisible by 3, print Crackle instead of the number.
If it's divisible by 5, print Pop. If it's divisible by both 3 and 5,
print CracklePop. You can use any language.
*/
/// any number x where x % y is divisible by that number. We'll use that fact to match boolean patterns in Scala.
//// In Scala RePL, copy-paste the function first. Then use the function in a second call from the REPL.
def listEval( number: Int ): String = (number % 3, number % 5 ) match {
case( 0, 0 ) => "CracklePOP"
case(0, _ ) => "Crackle"
case( _, 0) => "Pop"
case _ => number.toString()
}
val myList = (1 to 100) map {listEval(_)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment