Skip to content

Instantly share code, notes, and snippets.

@dcsobral
Created July 10, 2011 04:26
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 dcsobral/1074264 to your computer and use it in GitHub Desktop.
Save dcsobral/1074264 to your computer and use it in GitHub Desktop.
error: forward reference extends over definition of value next
def iterationForCoin(stream: Stream[Int], coin: Int) = {
val (lower, higher) = stream splitAt coin
val next: Stream[Int] = lower #::: (higher zip next map { case (a, b) => a + b })
next.toList
}
val coins = List(1, 2, 5, 10, 20, 50, 100, 200)
coins.foldLeft(1 #:: Stream.fill(200)(0))(iterationForCoin).last
@dcsobral
Copy link
Author

Ok, solved it like this:

class IterationForCoin(stream: Stream[Int], coin: Int) {
    val (lower, higher) = stream splitAt coin
    val next: Stream[Int] = lower #::: (higher zip next map { case (a, b) => a + b })
}
val coins = List(1, 2, 5, 10, 20, 50, 100, 200)
val result = coins.foldLeft(1 #:: Stream.fill(200)(0)){
     (stream, coin) => new IterationForCoin(stream, coin).next
}.last

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment