Created
July 26, 2013 18:20
-
-
Save gigamonkey/6091053 to your computer and use it in GitHub Desktop.
Change counting problem from SICP in Scala
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
object Change { | |
val zeros = Stream.continually(0) | |
def ways(denominations: Seq[Int]) = { | |
denominations.foldLeft(zeros) { (previous, d) => | |
lazy val base: Stream[Int] = zeros.take(d - 1) ++ 1 #:: previous.zip(base).map { x => x._1 + x._2 } | |
base.drop(d) | |
} | |
} | |
def main(args: Array[String]) { | |
val nums = args.map(_.toInt) | |
val amount = nums.head | |
val denominations = nums.tail | |
println(ways(denominations)(amount)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment