Skip to content

Instantly share code, notes, and snippets.

@aubreylouw
Created April 29, 2013 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubreylouw/5479552 to your computer and use it in GitHub Desktop.
Save aubreylouw/5479552 to your computer and use it in GitHub Desktop.
99 Scala problems -> problem 15
def duplicateN [X] (n:Int, xs:List[X]) : List[X] = (n, xs) match {
case (n, xs) if n < 0 => throw new RuntimeException ("illegal op: negative N")
case (_, Nil) => xs
case (0|1, xs) => xs
case (_, h::t) => (h::Nil) ::: copyN(n-1, h::Nil) ::: copyN(n,t)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment