Created
April 29, 2013 03:27
-
-
Save aubreylouw/5479552 to your computer and use it in GitHub Desktop.
99 Scala problems -> problem 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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